简体   繁体   中英

unable to access document name in node.js set by as current user email address from fire base

Set document name as current user email address from flutter application how can I access same documents name in node.js file.

Data set from flutter application in fire base code

FirebaseAuth.instance.currentUser().then((user){
  Firestore.instance
      .collection('cards')
        .document(user.email)
      .collection('tokens').add({
  'email': 'test@email.com',
  '
  }).then((val){
  });

});

I want get it in nodejs file how can I access in this line? what should I write in user.email to access the document name. Do I need to check user authentication in node.js?

exports.StripeSource =functions.firestore.document('cards/{user.email}/tokens/{tokenid}')......

I can get document name data doing so but only static document name but if I change that to user.email or user.uid (change according to current user email address or user id not same for all users) so I am understanding how can I get this type of document name.

exports.StripeSource =functions.firestore.document('cards/{data}/tokens/{tokenid}').onCreate(async (tokenSnap,context) => {
  const user_id = context.params.data;
  console.log('Document name:', user_id);....
..
}

The Node.js snippet you posted would be how you would create a Cloud Function to listen to changes on documents in the collection. What you probably want is the Admin SDK which will let you access documents. The SDK works much like the client SDK:

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

const snap = await admin.firestore()
  .collection('cards').doc(email)
  .collection('tokens').doc(tokenId).get();

console.log(snap.data());

As another note, unless you have specific reasons for doing so I'd recommend keying documents off of the uid , not the email , of a user. This is a friendlier format for URLs and will stay the same even if the user changes their email address.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM