最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeError: admin.firestore(...).collection(...).doc(...).collection(...).doc(...).get.then is not a function - Stac

programmeradmin2浏览0评论

I'm trying to code a function that triggers when a user gets a new ment and sends a notification.Comments are stored in /Users/{userID}/Notifications/{notificationID}. Users save their device notification tokens to /users/{userID}

This is the whole index.js file

'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()mentUID;
        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);

        });

    });

});

And here's the package.json file. Everything is up-to-date

{
  "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 gives the following error:

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)

I'm trying to code a function that triggers when a user gets a new ment and sends a notification.Comments are stored in /Users/{userID}/Notifications/{notificationID}. Users save their device notification tokens to /users/{userID}

This is the whole index.js file

'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().mentUID;
        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);

        });

    });

});

And here's the package.json file. Everything is up-to-date

{
  "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 gives the following error:

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)
Share Improve this question edited Dec 5, 2019 at 8:54 Peter Haddad 80.9k25 gold badges145 silver badges146 bronze badges asked Apr 7, 2018 at 19:57 Mahmoud EidarousMahmoud Eidarous 1741 gold badge2 silver badges17 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 12

Change this:

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

into this:

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.

more info here:

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

use this

 var response = await admin.firestore().collection('Users').doc(to_user_id).collection('Notifications').doc(notification_id).get();
 response.get("field");

try this.

var response = await admin.firestore().collection('Users').doc(to_user_id).collection('Notifications').doc(notification_id).get();

 const from_user_id =response.get("mentUID");

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论