6

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?

Mic
  • 331
  • 1
  • 2
  • 14

3 Answers3

15

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,

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thank your comment. Still a bit confused. I get NaN as a response. and firebase log show nothing about the name. which is odd. What could this mean? – Mic Oct 07 '18 at 02:40
  • edit. scratch that. it is. how ever. its now pritining it in the log but not allowing const givename = "code here" to work. or is this just not possible? – Mic Oct 07 '18 at 02:42
  • I have no idea what that last bit means. But as I said in my answer: the data from `doc.data()` will only be available inside the `then()` callback. See https://medium.com/google-developers/why-are-firebase-apis-asynchronous-callbacks-promises-tasks-e037a6654a93 for more on this way of programming, which is incredibly common in modern JavaScript. – Frank van Puffelen Oct 07 '18 at 02:44
  • Ah i see so if i want my code to trigger the name it needs to be within then() so i can use a const for this function to call it when ever i want.. what a bummer. ill fix this issue now and see if i can find a work around :) – Mic Oct 07 '18 at 02:46
  • There is no general workaround: learning how async works is by far the best you can do. The closest you can get is using a modern environment and `await` like in Francisco's answer. But if you don't understand asynchronous calls, that too is a leaky abstraction at times. – Frank van Puffelen Oct 07 '18 at 02:54
  • How would i use Await instead of .then i tried .await or await but dont work. iv installed npm i await and its correctly set up. – Mic Oct 07 '18 at 03:01
  • 1
    `await` is a keyword introduces in ES2017 (iirc). It's available in Node 8 and later. If you want to try using that, I recommend doing a few searches on "using await in node.js", as they're likely to turn up better explanations than what I can share quickly. – Frank van Puffelen Oct 07 '18 at 03:05
4

Haven't worked with Firestore yet, but from reading https://firebase.google.com/docs/firestore/query-data/get-data

something like below should work (untested):

const userRef = db.collection("users").doc("1234")
const userDoc = await userRef.get()
const {name} = userDoc.data()
console.log(name)
Cisco
  • 20,972
  • 5
  • 38
  • 60
0

Another way is:

const givename = await db.collection("users").doc("YOUR DOCUMENT NAME").get();

console.log(givename.data().name);

Remember to put this code in an async function since we are using await here

J.F.
  • 13,927
  • 9
  • 27
  • 65
Saadi
  • 121
  • 1
  • 8