Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS, Firestore get field

enter image description here

I have firestore set up on my nodejs chatbot. And its successfully copying the Facebook users ID to .doc Name, and setting there username and other info under there user ID in firestore

How do i go about retrieving this "field" name to display back to user.

what i have so far is this

 const givename = db.collection('users').doc(''+ sender_id).get("name");  
 
 

How ever firebase is returning nothing.

Iv looked alot online on how to return a field. but with little luck. Any suggestions?

like image 798
Mic Avatar asked Dec 03 '22 10:12

Mic


1 Answers

Calling get() doesn't return the data immediately, since the data is loaded asynchronously. It instead returns a promise, which resolves once the data is loaded. This means that you can use await (as in Francisco's answer) or (if await is not available) implement the then() method:

db.collection('users').doc(''+ sender_id).get().then(function(doc) {
  console.log(doc.data().name);
});

A common pitfall to be aware of: the doc.data() is only available inside the callback,

like image 51
Frank van Puffelen Avatar answered Dec 26 '22 21:12

Frank van Puffelen