Not able to access a specific field from a collection in Firebase
Are you sure the document you want to read exists in the colletion 'cards' under the desired ID? Check it on the firebase console.
Because I think you have an error in your saving data code: the add()
is used directly on a collection so you insert a document with a random generated ID. But you have the ID so you should use setData()
on the specific document.
Inserting a new document with random ID:
Firestore.instance.collection('cards').add({'Email': user.email});
Inserting a new document with a known ID (user.uid):
Firestore.instance.collection('cards').document(user.uid).setData({'Email': user.email});
DmDev
Updated on December 15, 2022Comments
-
DmDev over 1 year
while writing a Cloud Function I am trying to access
Email
field from collectioncards
. InFirebase
I make a new user and signin from that user and save that user email address to Firebase database by usingFirebaseAuth.instance.currentUser()
and make a document name.document(user.uid)
now I am accessing this user id in node.js in this codeconst snapshot=await firestore.collection('cards').doc(context.params.userId).get(); const customerEmailaddress= snapshot.data().Email;
but I am unable to access that email and by using this code. flutter application code is this which is saving data to firebase database
class PaymentServices{ addCard(token){ FirebaseAuth.instance.currentUser().then((user){ Firestore.instance .collection('cards') .document(user.uid) .collection('tokens').add({ 'tokenid': token.tokenId, 'Email': user.email, 'customerid': 'new' }).then((val){ }); print("saveedd"); }); } }
Cloud functions Data is succesfully inserted in firebase I just want access it from firebase and store values in variables like in customerEmailaddress
const functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.initializeApp(functions.config().firebase); const firestore= admin.firestore(); const stripe = require('stripe')('sk_test_.........'); exports.StripeSource =functions.firestore.document('cards/{userid}/tokens/{tokenid}').onCreate(async (tokenSnap,context) => { var customerdata; const data=tokenSnap.after.data(); const snapshot=await firestore.collection('cards').doc(context.params.userId).get(); const customerEmailaddress= snapshot.data().Email; const customerid= snapshot.data().custid; customerdata = await stripe.customers.create({ email: customerEmailaddress, }) });
Simple Example it might be something wrong with user.id case but this code is also not fetching the data from firebase in snapshot variable
exports.StripeSource =functions.firestore.document('data/{card}/tokens/{tokenid}').onCreate(async (tokenSnap,context) => { const snapshot = await firestore.collection('data').doc('card').get(); const customerEmailaddress= snapshot.data().Email; const customer = stripe.customers.create({ email: customerEmailaddress, }) .then((customer) => { return stripe.customers.createSource(customer.id, { source: 'tok_visa', }); }) });
-
DmDev over 4 yearsI am making a document using user.uid(firebase autogenrted id). and trying to access user Id in node.js by using this const snapshot=await firestore.collection('cards').doc(context.params.userId).get(); is my code is ok till this line? is this a way to access user.uid from firebase in node.js? I am little bit confuse
-
Erick over 4 yearsYou should show us where you are using that code and what kind of function it is to see if its right or not. When you say access user.uid from node.js, it depends on the function context and if the user is the one executing the function or not (reference). The way you are using it (context.params.userId) is for a Database Triggered Function where userId is a wildcard in the path on the ref(), check the link from before.
-
Erick over 4 yearsTheres a typo with the wildcard userId: on your ref's path you put 'userid', and then you are recovering 'userId' (with capital I)
-
DmDev over 4 yearsthanks Erick even my this code is not working. when I put static email address it make customer on stripe account but when I retrieve data from fire base it did not work here. here is the simple code please check it I update the code in the last of question with name Simple Example
-
Erick over 4 yearsok, thats weird, all I can think is maybe the async/await its not working properly, but if you are using Node 8 for the runtime it should work. Make sure you are using Node 8 for runtime on your 'package.json' or remove async/await and try to get the data using traditional promises with then() and catch() and print the error if there is any.