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

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

问题描述

我在我的 nodejs 聊天机器人上设置了 firestore.它成功地将 Facebook 用户 ID 复制到 .doc Name,并在 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.

到目前为止我所拥有的是这个

what i have so far is this

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

firebase 怎么没有返回任何东西.

How ever firebase is returning nothing.

我在网上看了很多关于如何返回字段的信息.但运气不佳.有什么建议吗?

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

推荐答案

调用 get() 不会立即返回数据,因为数据是异步加载的.相反,它返回一个承诺,一旦加载数据就会解决.这意味着您可以使用 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天全站免登陆