【问题标题】:Error: no entity to update: app - Firestore Cloud Function错误:没有要更新的实体:app - Firestore Cloud Function
【发布时间】:2018-03-22 10:18:45
【问题描述】:

这是我最基本的云功能:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
const db = admin.firestore()

exports.createdNewAccount = functions.auth.user().onCreate(event => {
    return db.collection('users').doc(event.data.uid).update({
        "creationDate" : Date.now()
    })
})

我得到了错误

错误:没有要更新的实体:应用程序

我的代码有什么问题?

【问题讨论】:

    标签: firebase google-cloud-functions google-cloud-firestore


    【解决方案1】:

    很可能,event.data.uid 的文档不存在。 update() 的文档指出:

    如果应用于不存在的文档,更新将失败。

    请改用set()

    【讨论】:

    • 你可能真的想用 merge 调用 set - 文档示例是:cityRef.set({ capital: true }, { merge: true })
    • 事情是,firebase 让你更新如果不存在而 firestore 不存在...
    • @RonRoyston 'Firebase rtdb' :)
    • update 和 set with merge:true 有什么区别呢?
    • 这为我解决了这个问题。必须在提交实体之前调用onCreate。因此,当您进行搜索/更新时,它会失败,因为该实体尚不存在。 set 工作正常,因为您实际上是在创建实体,编写您想要的任何字段。
    【解决方案2】:

    在使用 Firebase 模拟器在本地测试我的应用时,我遇到了类似的错误。我的 firebase 配置文件如下所示:

    import firebase from "firebase";
    import "firebase/firestore";
    
    const firebaseConfig = {
      apiKey: <FIREBASE_API_KEY>,
      authDomain: <FIREBASE_AUTH_DOMAIN>,
      databaseURL: <FIREBASE_DB_URL>,
      projectId: <FIREBASE_PROJECT_ID>,
      storageBucket: <FIREBASE_STORAGE_BUCKET>,
      messagingSenderId: <FIREBASE_MSG_SENDER_ID>,
      appId: <FIREBASE_APP_ID>,
      measurementId: <FIREBASE_MEASUREMENT_ID>,
    };
    
    // Initialize Firebase
    const firebaseApp = firebase.initializeApp(firebaseConfig);
    // for local instances, use the emulator to connect the frontend to firestore & functions
    if (location.hostname === "localhost") {
      firebase.firestore().settings({
        host: "localhost:8080",
        ssl: false,
      });
    
      firebase.functions().useFunctionsEmulator("http://localhost:5001");
    }
    

    原来我的本地 Firestore 数据库(预计在 localhost:8080 运行)没有被击中。所以我的云函数试图写入一个不存在的数据库。根本问题是我的后端也初始化到不同的数据库:

    // functions/index.js
    const admin = require("firebase-admin");
    var serviceAccount = require("./serviceAccount.json");
    
    admin.initializeApp({
      credential: admin.credential.cert(serviceAccount),
      databaseURL: "https://other-firebase-project-id.firebaseio.com",
    });
    

    解决方案(最初是 adapted from a Fireship tutorial)是完全删除这个 [不正确的] 重新初始化数据库:

    // functions/index.js
    ... 
    admin.initializeApp();
    ... 
    

    毕竟,according to the docs,我们可以在没有参数的情况下初始化 Firebase Admin SDK,因为FIREBASE_CONFIG 环境变量自动包含在 Cloud Functions 中,用于通过 Firebase CLI 部署的 Firebase 函数。 p>

    FWIW,请务必在 CLI 上设置正确的 Firebase 项目。这样做可以让 Firebase CLI 将 Functions/Firestore 挂接到正确的项目:

    firebase use <desired-firebase-project-id>
    

    如果您有多个 Firebase 项目,您可以通过以下方式列出它们:firebase projects:list

    【讨论】:

      猜你喜欢
      • 2018-06-06
      • 1970-01-01
      • 2021-10-28
      • 2020-09-16
      • 1970-01-01
      • 2019-09-14
      • 2020-07-02
      • 2019-05-07
      相关资源
      最近更新 更多