NodeJS,Firestore获取字段 [英] NodeJS, Firestore get field
问题描述
我在nodejs聊天机器人上设置了Firestore. 并且成功将Facebook用户ID复制到.doc名称, 并在Firestore中的用户ID下设置用户名和其他信息
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.
我到目前为止有什么
const givename = db.collection('users').doc(''+ sender_id).get("name");
firebase却什么也没返回.
How ever firebase is returning nothing.
Iv在网上看了很多关于如何返回字段的信息.但运气不好. 有什么建议吗?
Iv looked alot online on how to return a field. but with little luck. Any suggestions?
推荐答案
调用get()
不会立即返回数据,因为数据是异步加载的.相反,它返回一个Promise,一旦加载数据就解决.这意味着您可以使用await
(如弗朗西斯科的回答)或(如果await
不可用)实施then()
方法:
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);
});
要注意的一个常见陷阱:doc.data()
仅在回调内部可用,
A common pitfall to be aware of: the doc.data()
is only available inside the callback,
这篇关于NodeJS,Firestore获取字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!