in NodeJS I have the following code inside a function:
var request = require('request-promise');
exports.quotetest = functions.https.onRequest(async (req, res) => {
// ... code ...
await request(options, function (error, response, body) {
const jsondata = JSON.parse(body).response;
firestore.collection('Partite').doc('Serie A').update({
[m] : {"home":jsondata[0].bookmakers[0] }
});
})
}
And it gives me the following error: TypeError: Cannot read properties of undefined (reading 'bookmakers')
. I think because when I update Firestore jsondata
isn't ready yet. How could I wait for jsondata to be ready? If I try const jsondata = await JSON.parse(body).response;
I receive the following error: SyntaxError: await is only valid in async functions and the top level bodies of modules