Google-api-nodejs-client: TypeError: Converting circular structure to JSON

Created on 3 Feb 2018  路  3Comments  路  Source: googleapis/google-api-nodejs-client

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

Most helpful comment

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!

All 3 comments

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!

@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)
}
````

@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.

https://github.com/axios/axios/pull/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.)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nanu-c picture nanu-c  路  3Comments

lowagner picture lowagner  路  3Comments

joseparoli picture joseparoli  路  3Comments

JustinBeckwith picture JustinBeckwith  路  3Comments

streamnsight picture streamnsight  路  4Comments