使用Firestore云功能

时间:2018-05-10 12:44:01

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

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

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
} 

我缺少什么?请提示

1 个答案:

答案 0 :(得分:1)

您需要使用Firebase Admin SDK来访问超出触发功能范围的Firestore集合。通常情况如下:

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