admin管理员组文章数量:1026707
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");
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?
Share Improve this question edited Oct 7, 2018 at 1:47 Mic asked Oct 7, 2018 at 1:41 MicMic 3331 gold badge2 silver badges14 bronze badges3 Answers
Reset to default 15Calling 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,
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)
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
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");
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?
Share Improve this question edited Oct 7, 2018 at 1:47 Mic asked Oct 7, 2018 at 1:41 MicMic 3331 gold badge2 silver badges14 bronze badges3 Answers
Reset to default 15Calling 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,
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)
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
本文标签: javascriptNodejsFirestore get fieldStack Overflow
版权声明:本文标题:javascript - NodeJS, Firestore get field - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1739147242a1625722.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论