我尝试根据为HTTP请求传递的查询值创建一个Firebase Cloud 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)});
});
我进行批量更新,如果成功,我想返回200状态。 但是,Firebase日志显示“功能执行耗时868毫秒,状态为'crash'”。