NodeJS,Firestore获取字段 [英] NodeJS, Firestore get field

查看:64
本文介绍了NodeJS,Firestore获取字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆