I try to make a Firebase Cloud Function that updates some references, according to the query values passed for the HTTP request.
Here's my function:
exports.saveBuy = functions.https.onRequest((req, res) => {
const shopID = req.query.shopID;
const userID = req.query.userID;
const shopRef = db.collection('Shops').doc(shopID)
const userRef = db.collection('Users').doc(userID)
const increment = firebase.firestore.FieldValue.increment(1)
var batch = db.batch();
batch.update(shopRef, ({count: increment}));
batch.update(shopRef, ({clients: firebase.firestore.FieldValue.arrayUnion(userId)}));
batch.update(userRef, ({purchases: firebase.firestore.FieldValue.arrayUnion(shopId)}));
// Commit the batch
batch.commit().then(function () {
console.log('success')
res.status(200).send()
return null
}).catch(error => { res.status(500).send();
return console.error(error)});
});
I make batch updates, and I want to return a 200 status in case of success. However, the Firebase Log shows me "Function execution took 868 ms, finished with status: 'crash'"...