Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 683
Raim Khalil Avatar asked Jul 09 '18 00:07

Raim Khalil


2 Answers

use doc() function instead of document()

like image 84
saigopi.me Avatar answered Nov 14 '22 11:11

saigopi.me


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) ...
like image 31
Arman Charan Avatar answered Nov 14 '22 09:11

Arman Charan