这是我的云功能代码
exports.markThemAllRead = functions.https.onCall((data) => {
const user = {
id: data.id,
}
let batch = admin.firestore().batch();
admin.firestore().collection("Notifications").doc("comments").collection(user.id).get().then(querySnapshot => {
if(querySnapshot.empty){
return "nice"
}else {
querySnapshot.forEach(doc=>{
batch.update(doc.ref, {isRead:true})
});
return batch.commit()
}
}).catch(err => {
console.log(err)
})
return "Good"
})
我尝试了很多return语句的组合,但是却不断出错:响应不是有效的JSON对象。谁能指导我解决这个问题?我的代码中还有许多其他的onCall函数,但这是与batch.update()一起使用的唯一函数...也许与此有关?
编辑尝试
尝试此操作时仍然出现相同的错误:
exports.markThemAllRead = functions.https.onCall((data) => {
const user = {
id: data.id,
}
return markThemRead(user)
})
async function markThemRead(user){
let batch = admin.firestore().batch();
const docsRef = admin.firestore().collection("Notifications").doc("comments").collection(user.id)
const docs = await docsRef.get();
docs.forEach(function(doc){
batch.update(doc.ref, {isRead:true})
})
return batch.commit()
}
基于Firebase文档中的Sync,async和promises示例
async function markThemRead(user){
let batch = admin.firestore().batch();
const docsRef = admin.firestore().collection("Notifications").doc("comments").collection(user.id)
const docs = await docsRef.get();
docs.forEach(function(doc){
await batch.update(doc.ref, {isRead:true})
})
return batch.commit().then(function () { return {status: "All Good"}})
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句