gpt4 book ai didi

reactjs - 错误 : Response is not valid JSON object on firebase function with onCall

转载 作者:行者123 更新时间:2023-12-05 04:56:28 26 4
gpt4 key购买 nike

这是我的云函数代码

 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 and 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"}})

}

关于reactjs - 错误 : Response is not valid JSON object on firebase function with onCall,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64923075/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com