Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Converting circular structure to JSON #983

Closed
eduDorus opened this issue Feb 3, 2018 · 4 comments
Closed

TypeError: Converting circular structure to JSON #983

eduDorus opened this issue Feb 3, 2018 · 4 comments
Assignees

Comments

@eduDorus
Copy link

eduDorus commented Feb 3, 2018

Hei Devs,

In the newest version 25 there is a bug in the CloudML prediction response. The response has circular structure and can not be converted to JSON. Here my code:

const google = require('googleapis');
const ml = google.ml('v1');

function auth(callback) {
    google.auth.getApplicationDefault(function (err, authClient) {
        if (err) {
            return callback(err);
        }
        if (authClient.createScopedRequired && authClient.createScopedRequired()) {
            // Scopes can be specified either as an array or as a single,
            // space-delimited string.
            authClient = authClient.createScoped([
                'https://www.googleapis.com/auth/cloud-platform',
            ]);
        }
        callback(null, authClient);
    });
}

/**
 * @param {Function} callback Callback function.
 */
function getMLPrediction(data, callback) {
    auth(function (err, authClient) {
        if (err) {
            return callback(err);
        }
        ml.projects.predict({
            auth: authClient,
            name: 'projects/***/models/census',
            resource: data,
        }, (e, prediction) => {
            if (e) {
                return callback(e);
            }
            console.log(JSON.stringify(prediction)); <--------- This will crash
            console.log('Prediction:', prediction);
            callback(null, prediction);
        });
    });
}

I reverted back to ^24.0.0 and everything works fine.

Kindly Regards,
eduDorus

@JustinBeckwith
Copy link
Contributor

Greetings! The callback returns a response object, which has a data field. Instead of this:

}, (e, prediction) => {
            if (e) {
                return callback(e);
            }
            console.log(JSON.stringify(prediction)); <--------- This will crash
            console.log('Prediction:', prediction);
            callback(null, prediction);
        });

Do this!

}, (e, res) => {
            if (e) {
                return callback(e);
            }
            console.log(res.data); <--------- This will work :)
            console.log('Prediction:', res.data);
            callback(null, res.data);
        });

Hope this helps!

@uptownhr
Copy link

uptownhr commented May 7, 2018

@JustinBeckwith Hi justin, im' using the promise interface instead of the callback. Do you happen to know how to handle that?

const serviceRequest = calendarService.events.list({
    calendarId: calendar_id,
    timeMin: (new Date()).toISOString(),
    maxResults: 50,
    singleEvents: true,
    orderBy: 'startTime'
  })

  try {
    await serviceRequest
  } catch (e) {
    if (e.status) res.status(e.status).send(e.statusText)
    logger.error(e)
    res.status(500).send(e)
  }

@TimJohns
Copy link

TimJohns commented Jun 26, 2018

@uptownhr If you're seeing the exception when you call logger.error(e), I wonder if your logger is calling JSON.stringify on 'e'. I submitted a PR to Axios regarding this, that might be informative.

axios/axios#1625

In your case, you might be able to work around with this or something similar:

logger.error({message: e.message, stack: e.stack})

It's apparently really hard for me to remember to do that, and error handling is ripe for missing code coverage, so I submitted the PR above. (Importantly, separately, the request headers in the AxiosErrors serialized by console.log() contain bearer tokens, and I don't like to see those in the logs.)

@Wandersonelias
Copy link

i resolve, this problem passed the value variable of return for variable e next return res.json(variable)
Exmaple:
let result = await api.post('/getnfe',payload); var intermediaria = result.data; res.status(200).json(intermediaria);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants