TypeError:admin.firestore.collection不是函数

Kiv*_*men 3 node.js firebase google-cloud-functions google-cloud-firestore

我需要帮助,但我总是在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);
        });
    });
});
Run Code Online (Sandbox Code Playgroud)

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
}
Run Code Online (Sandbox Code Playgroud)

我得到的错误

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)
Run Code Online (Sandbox Code Playgroud)

Dou*_*son 6

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

  • 你是我的救星,我不敢相信我没有看到这一点,因为我检查了其他 stackoverflow 帖子,其中有人遇到了与我相同的问题,但错过了 admin.firestore() 函数调用。非常感谢! (2认同)