我正在尝试更新Firestore中的现有文档。该文件肯定存在。从客户端发送的数据具有来自文档本身的所有数据(每个字段和值以及由Firestore自动生成的文档ID)。我已经尝试过将数据的每个值传递给firebase函数:data.id(自动生成ID),data.email,data.name,以创建对要更新的文档的引用,但仍会得到错误响应。但是,每次触发该函数时都会执行.then和.catch块。
正如我之前所说,该文档存在于“用户”集合中的数据库中。我该如何从admin SDK中引用它?
不确定我是否只是以错误的方式格式化查询。
这里是被调用的函数:
exports.createAssetMux = functions.https.onCall((data, context) => {
admin.firestore().collection('users').doc(data.id).update({
streamID: '98273498237'
}).then(
console.log('update success')
).catch(error => {
console.log('error message: ', error)
});
});
如documentation中所述,您需要返回一个承诺,该承诺将解决您要发送回客户端的数据。如果您在不处理promise的情况下开始了一些异步工作(update()
返回promise),则该函数将关闭并且工作将无法完成。
return admin.firestore().collection('users').doc(data.id).update({
streamID: '98273498237'
}).then(
console.log('update success')
return { your: "response" }
).catch(error => {
console.log('error message: ', error)
return { your: "error" }
});
您应该返回对您的客户端应用有意义的响应。
为了编写有效的云功能,您肯定需要了解JavaScript的承诺如何工作。