javascript - TypeError : admin. firestore(...).collection(...).doc(...).collection(...).doc(...).get.then 不是函数

标签 javascript node.js firebase google-cloud-firestore firebase-cloud-messaging

我正在尝试编写一个函数,该函数在用户收到新评论并发送通知时触发。评论存储在 /Users/{userID}/Notifications/{notificationID} 中。用户将他们的设备通知 token 保存到 /users/{userID}

这是整个 index.js 文件

'use strict';

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

exports.sendNotification = functions.firestore.document('Users/{userID}/Notifications/{notificationID}').onWrite((data, context) => {

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


    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(to_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 experimental:functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "dependencies": {
    "firebase-admin": "~5.11.0",
    "firebase-functions": "^1.0.0"
  },
  "devDependencies": {
    "eslint": "^4.12.0",
    "eslint-plugin-promise": "^3.6.0"
  },
  "private": true
}

Firebase 给出以下错误:

TypeError: admin.firestore(...).collection(...).doc(...).collection(...).doc(...).get.then is not a function
        at exports.sendNotification.functions.firestore.document.onWrite (/user_code/index.js:19:119)
        at Object.<anonymous> (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:112:27)
        at next (native)
        at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71
        at __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12)
        at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:82:36)
        at /var/tmp/worker/worker.js:700:26
        at process._tickDomainCallback (internal/process/next_tick.js:135:7)

最佳答案

改变这个:

admin.firestore().collection('Users').doc(to_user_id).collection('Notifications').doc(notification_id).get.then(queryResult => {

进入这个:

admin.firestore().collection('Users').doc(to_user_id).collection('Notifications').doc(notification_id).get().then(queryResult => {

get() returns Promise containing non-null firebase.firestore.DocumentSnapshot

Reads the document referred to by this DocumentReference.

更多信息在这里:

https://firebase.google.com/docs/reference/js/firebase.firestore.DocumentReference#get

关于javascript - TypeError : admin. firestore(...).collection(...).doc(...).collection(...).doc(...).get.then 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49711435/

相关文章:

javascript - 如何根据javascript中另一个数组的值对数组进行排序

javascript - DateTimePicker 值与显示日期不一致

javascript - 如何通过深层路径在对象数组中查找对象?

javascript - 为什么这个正则表达式替换在开头而不是在结尾删除符号?

node.js - Passport req.logOut() 函数不起作用?

javascript - Discord 机器人没有获得所有用户

javascript - 连接mongodb到 Node 报错

android - 使用 Firebase Auth : Developer Error 登录 Facebook

java - 为什么我的 Cloud Run - 到 Firebase 调用需要这么长时间?

iOS APNS 证书即将过期(服务器端的 Firebase)