Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get an specific field data from cloud fire store database?

I am able to access data document data but unable to access further field data of card like email. getting error on .get() function.

enter image description here

Function

exports.StripeSource =functions.firestore.document('data/{card}/tokens/{tokenid}').onCreate(async (tokenSnap,context) => {
  const user_id = context.params.card;
  console.log('Document data:', user_id);
  var customerdata;
  const snapshot =  firestore.collection('test').doc('card');
  return snapshot
      .get()
      .then(doc => {
        if (!doc.exists) {
          console.log('No such User document!');
          console.log('Document data:', doc.data().email);

        } else {
          console.log('Document data:', doc.data());
          console.log('Document data:', doc.data().email);
          return true;
        }
      })
      .catch(err => {

        console.log('Error getting document', err);
        return false;
      });
  });

I run a code and get this error in console

[![TypeError: snapshot.get is not a function
    at exports.StripeSource.functions.firestore.document.onCreate (/srv/index.js:13:8)
        at cloudFunction (/srv/node_modules/firebase-functions/lib/cloud-functions.js:131:23)
        at /worker/worker.js:825:24
        at <anonymous>
        at process._tickDomainCallback (internal/process/next_tick.js:229:7)][2]][2]
like image 639
Developer Avatar asked Dec 04 '19 15:12

Developer


People also ask

How do I get node data from firestore?

To read your data from Firestore, use the get() method. To read from a collection, specify a collection() before calling get() . Or, if you need to read from a document, specify a doc() before calling get() . // get collection const users = await db.


1 Answers

If you want to read/write from/to Firestore in your Cloud Function you need to use the Admin SDK and you have to do:

const snapshot =  admin.firestore.collection('test').doc('card');

instead of

const snapshot =  firestore.collection('test').doc('card');

Note that you need to import the Admin SDK module using Node require statements. Add these lines to your index.js file:

// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
const functions = require('firebase-functions');

// The Firebase Admin SDK to access Firestore.
const admin = require('firebase-admin');
admin.initializeApp();

Note also that calling the variable snapshot may lead to some errors (i.e. mixing-up variable type due to wrong naming).

By doing admin.firestore.collection('test').doc('card'); you define a DocumentReference. It's by calling the asynchronous get() method that you get a DocumentSnapshot.


Finally, do not forget to return a Promise or a value in your Cloud Function (you don't return anything if !doc.exists). It's worth watching the 3 videos about "JavaScript Promises" from the Firebase video series: https://firebase.google.com/docs/functions/video-series/

like image 74
Renaud Tarnec Avatar answered Sep 23 '22 16:09

Renaud Tarnec