首页 > 解决方案 > 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: {}
  }
}

标签: javascriptfirebasegoogle-cloud-firestoregoogle-cloud-functionsfirebase-admin

解决方案


文档中所述,您需要返回一个 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 是如何工作的。


推荐阅读