TypeError,admin.firestore(...)。collection(...)。doc(...)。collection(...)。doc(...)。get.then不是函数

时间:2019-01-01 14:01:52

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

我正在尝试编写一个函数,该函数在用户获得新评论并发送通知时触发。评论存储在/ Users / {user_id} / Notifications / {notification_id}中。用户将其设备通知令牌保存到/ users / {userID}

'use strict'

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

exports.sendNotif = functions.firestore.document('Users/{user_id}/Notifications/{notification_id}')
.onWrite((change,context) =>
{
    const user_id = context.params.user_id;
    const notification_id = context.params.notification_id;

    return admin.firestore().collection('Users').document(user_id).collection('Notifications').document(notification_id).get().then(queryResult=>{

      const from_user_id = queryResult.data().From;

      const from_data = admin.firestore().collection('Users').document(from_user_id).get();
      const to_data = admin.firestore().collection('Users').document(user_id).get();

      return Promise.all([from_data, to_data]).then(result => {

        const from_name = result[0].data().From;
        const to_name = result[1].data().From;

        console.log("FROM: " + from_name + "TO: " + to_name);

      });

    });

});

这是package.json文件。一切都是最新的

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "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": "^6.4.0",
    "firebase-functions": "^2.1.0"
  },
  "private": true
}

Firebase出现以下错误:

TypeError: admin.firestore(...).collection(...).document is not a function
    at exports.sendNotif.functions.firestore.document.onWrite (/user_code/index.js:13:50)
    at cloudFunctionNewSignature (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:105:23)
    at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:135:20)
    at /var/tmp/worker/worker.js:768:24
    at process._tickDomainCallback (internal/process/next_tick.js:135:7)

1 个答案:

答案 0 :(得分:0)

admin.firestore().collection(...)返回一个CollectionReference对象。从链接的API文档中可以看到,该方法上没有名为document()的方法。但是有一种doc()方法。改用它。