Reputation: 789
I am able to access data document data but unable to access further field data of card like email. getting error on .get() function.
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]
Upvotes: 6
Views: 2158
Reputation: 1114
Nicely explained by @Renaud. Just a little change.
To make the code work instead of admin.firestore
, I had to use admin.firestore()
with the bracket.
Just a summary here. Instead of
const snapshot = firestore.collection('test').doc('card');
First we need to import firebase-admin:
const admin = require("firebase-admin");
admin.initializeApp();
and use it like this:
const snapshot = admin.firestore().collection('test').doc('card');
Working code for this problem. You can copy-paste this, it will run.
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
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;
var customerEmail;
//I have to assign the a value to customerData, to use it outside.
//That's why I made a little change and added await here
const snapshot = await admin.firestore()
.collection('test')
.doc('card')
.get()
.then((doc) => {
if (!doc.exists) {
console.log('No such User document!');
console.log('Document data:', doc.data());
} else {
console.log('Document data:', doc.data());
console.log('Document data:', doc.data().email);
//Assigning value to customerData here
customerData = doc.data();
customerEmail = doc.data().email;
return true;
}
})
.catch((err) => {
console.log('Error getting document', err);
return false;
});
//Now the value of customerData or customerEmail can be used anywhere
console.log(customerData, "customerData Extracted");
console.log(customerEmail, "customerEmail Extracted");
});
Upvotes: 0
Reputation: 83191
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/
Upvotes: 5