javascript - Admin SDK 更新返回代码 5:没有要更新的实体
问题描述
我正在尝试更新 firestore 中的现有文档。该文件肯定存在。从客户端发送的数据包含来自文档本身的所有数据(每个字段和值以及由 firestore 自动生成的文档 ID)。
我已经尝试了传递给 Firebase 函数的每个数据值:data.id (auto-gen id), data.email
, data.name
, 以创建对要更新的文档的引用,但仍然得到错误响应。但是,每次触发函数时都会执行 .then 和 .catch 块。
正如我之前所说,该文档存在于“用户”集合中的数据库中。我如何从管理 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)
});
});
这是我在终端中收到的错误消息:
error message: Error: 5 NOT_FOUND: no entity to update: app: "dev~firebase-app"
path <
Element {
type: "users"
name: "kPuVNXsFFIyhs3Qad06B"
}
>
code: 5,
details: 'no entity to update: app: "firebase-app"\n' +
'path <\n' +
' Element {\n' +
' type: "users"\n' +
' name: "kPuVNXsFFIyhs3Qad06B"\n' +
' }\n' +
'>\n',
metadata: Metadata {
internalRepr: Map(1) { 'content-type' => [Array] },
options: {}
}
}
解决方案
如文档中所述,您需要返回一个 Promise,该 Promise 使用您要发送回客户端的数据进行解析。如果你开始一些异步工作而不处理承诺(update()
返回一个承诺),那么函数将关闭并且工作不会完成。
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 Promise 是如何工作的。
推荐阅读
- r - 使用传单缩放时更新数据表(闪亮)
- django - 在模型查询集中如何获取反向外键记录之一
- php - 如何在 Windows 上的 PHP 7.3 中安装 BCMath
- android - 使用 Kotlin 尝试迭代 ChipGroup 的 Android Espresso 测试
- swift - TableView 单元格在 UITableViewController 中的 headerview 上滚动
- linux - x86_64 平台上的 Diolan DLN-2 SPI 控制器
- bash - 为 bash exec 命令创建别名
- javascript - 如何使用 PHP 正确回显 Notify.js 通知?
- javascript - 为什么从未定义的承诺返回字符串?
- go - 在 protobuf 文件中使用 google.protobuf.Value 并在结构中使用 go interface{} 字段,反之亦然