【问题标题】:Using a Firestore Cloud Functions使用 Firestore 云函数
【发布时间】:2018-05-10 12:44:01
【问题描述】:

我有这个问题。在我的代码中,取决于选择的项目名称,我想从另一个集合中检索文档。我正在使用

exports.updateUser = functions.firestore
    .document('Users/{userId}')
    .onUpdate((change, context) => {

      const newValue = change.after.data().currentProject;
      console.log("Project changed to - " + newValue);

      const previousValue = change.before.data().currentProject;
      console.log("from - " + previousValue);


      // ### FROM HERE DOES NOT WORK ###

      const projectId = firestore.collection('Projects')
            .where('name', '==', newValue).get()
            .then(doc => {
                console.log(doc.data().id);
                return doc.data().id;
            });
    });

我得到一个错误

ReferenceError: firestore is not defined
    at exports.updateUser.functions.firestore.document.onUpdate (/user_code/index.js:25:14)

所以也许我需要另一个依赖项? 我的 package.json 是

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "lint": "eslint .",
    "serve": "firebase serve --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "dependencies": {
    "firebase-admin": "~5.12.0",
    "firebase-functions": "^1.0.1"
  },
  "devDependencies": {
    "eslint": "^4.12.0",
    "eslint-plugin-promise": "^3.6.0"
  },
  "private": true
} 

我错过了什么?请给个提示

【问题讨论】:

    标签: node.js firebase google-cloud-firestore google-cloud-functions


    【解决方案1】:

    您将需要使用 Firebase Admin SDK 来访问触发您的函数的范围之外的 Firestore 集合。通常这看起来像:

    admin.firestore().collection('Projects')...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-04
      • 1970-01-01
      • 2021-04-11
      • 1970-01-01
      • 2021-02-13
      • 2019-08-28
      • 2018-03-28
      • 2021-03-17
      相关资源
      最近更新 更多