Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: admin.firestore.collection is not a function

I need help i always get the error message on firestore cloud functions admin.firestore.collection is not a function. What am i doing wrong have i forgotten something or do i need to change something.

Here is the code

'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
}

The error i get

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)
like image 905
Kivanc Özmen Avatar asked Dec 06 '18 12:12

Kivanc Özmen


2 Answers

It's admin.firestore(), not admin.firestore. It's a function call, not a property. In your code, you're doing it correctly once, then incorrectly twice.

like image 77
Doug Stevenson Avatar answered Nov 14 '22 22:11

Doug Stevenson


I had same problem for my functions.

exports.getScreams = functions.https.onRequest((req, res) => {
admin.firestore.collection('screams').get()
.then((data) => {
    let screams = [];
    data.forEach((doc) => {
        screams.push(doc.data())
    });
    return res.json(screams);
})
.catch((err) => console.error(err)) })

Then i use

admin.firestore() instead of admin.firestore

like image 45
akshay_sushir Avatar answered Nov 14 '22 22:11

akshay_sushir