TypeError:admin.firestore.collection不是函数

时间:2018-12-06 12:56:28

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

我需要帮助,但我始终会在firestore云功能admin.firestore.collection上收到错误消息。我在做什么错了我忘记了一些东西还是需要改变一些东西?

这是代码

'use strict'

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

exports.sendNotification = functions.firestore.document('users/{userID}/notifications/{notificationID}').onWrite((change, context) => {

    const to_user_id = context.params.userID;
    const notification_id = context.params.notificationID;

    console.log('We have notification from: ' + to_user_id + ' The notification id is: ' + notification_id);

    return admin.firestore().collection('users').doc(to_user_id).collection('notifications').doc(notification_id).get().then(queryResult => {

        const from_user_id = queryResult.data().commentUID;
        const from_user_data = admin.firestore.collection('users').doc(from_user_id).get();
        const to_user_data = admin.firestore.collection('users').doc(user_id).get();

        return Promise.all([from_user_data, to_user_data]).then(result => {

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

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

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": "6.3.0",
    "firebase-functions": "^2.1.0"
  },
  "devDependencies": {
    "eslint": "^5.9.0",
    "eslint-plugin-promise": "^4.0.1"
  },
  "private": true
}

我得到的错误

TypeError: admin.firestore.collection is not a function
    at admin.firestore.collection.doc.collection.doc.get.then.queryResult (/user_code/index.js:17:48)
    at process._tickDomainCallback (internal/process/next_tick.js:135:7)

2 个答案:

答案 0 :(得分:1)

它是admin.firestore(),而不是admin.firestore。这是一个函数调用,而不是属性。在您的代码中,您一次执行正确,然后两次执行不正确。

答案 1 :(得分:1)

我的功能遇到相同的问题。

hc_yAxis_multiples

然后我用

admin.firestore()代替admin.firestore