Raim Khalil
Raim Khalil

Reputation: 387

admin.firestore(...).document is not a function at exports

I am running a cloud function that is triggered by a firebase realtime database change and updates FireStore. However, although the function triggers, the function cannot access the Firestore.

exports.reveal = functions.database.ref('/reveals/{postIDthatWasRevealed}/revealed').onUpdate((change, context) => {
const revealedValue = change.after.val()

if (revealedValue === true) {
   var updates = {}
   const postID = context.params.postIDthatWasRevealed
   return admin.firestore().document('/posters/' + postID).get().then(querySnapshot => {

At this point, the console log states TypeError:admin.firestore(...).document is not a function at.. I have already tried this answer :Tried Answer But the problem continues. Does this have to do with the fact that I am accessing firestore inside of firebase cloud function? Are my cloud functions not properly updated?

Edit (Includes Initialization code)

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

In addition, I have tried to debug the function by changing it to :

if (revealedValue === true) {
   var updates = {}
   const postID = context.params.postIDthatWasRevealed

   return admin.firestore()

Now, when I trigger the function, I get Function execution took 811 ms, finished with status: 'ok' Thus, it seems as if the firestore function is valid, just that my syntax may be off or I may be forgetting something

Upvotes: 7

Views: 5666

Answers (2)

saigopi.me
saigopi.me

Reputation: 14918

use doc() function instead of document()

Upvotes: 10

Arman Charan
Arman Charan

Reputation: 5797

document() is a property of functions.firestore

The equivalent for admin.firestore() is collection().doc()

See Firestore: Get Data for more info.

admin.firestore().collection('posters').doc(postID) ...

Upvotes: 8

Related Questions