Reputation: 352
Hi I have a situation where I need to fetch the data from firestore in cloud function. But the UI is blocking because of the CORS issue hence I added the cors dependency in Nodejs. and when I need to fetch the data from the firestore I need await functionality but this is not possible inside cors. Hence need a solution here is the code piece.
const cors = require('cors')({ origin: true });
const Firestore = require('@google-cloud/firestore');
const PROJECTID = 'something';
const COLLECTION_NAME = 'ui_data';
const firestore = new Firestore({
projectId: PROJECTID,
timestampsInSnapshots: true,
});
exports.helloWorld = async function index(req, res) {
cors(req, res, () => {
const data = [];
const querySnapshot = await firestore.collection(COLLECTION_NAME).get();
querySnapshot.forEach(doc => {
data.push(doc.data());
});
return res.status(200).send(data);
});
}
Thanks in advance
Upvotes: 1
Views: 161
Reputation: 6919
You can't use async to external function.
exports.helloWorld = function index(req, res) {
cors(req, res, async () => {
const data = [];
const querySnapshot = await firestore.collection(COLLECTION_NAME).get();
querySnapshot.forEach(doc => {
data.push(doc.data());
});
return res.status(200).send(data);
});
}
Upvotes: 1