首页 > 解决方案 > firestore 获取字段以发挥作用

问题描述

所以我早些时候提出了一个关于从 Firestore 获取字段的问题。并且它在日志中正常工作。

但我试图找到一种方法将我的代码的获取名称设置为函数

这是代码

db.collection('users').doc('' + sender_id).get().then(doc=>{
    if (!doc.exists) {
        console.log('No Such document exists');
    }
    // do stuff with data smiley face.
    (doc.data().name);
    console.log('So it got your name correctly' + doc.data().name);
}).catch(err=>{
    console.error('error getting document', err);
    process.exit();
});

这就是我想要实现它的方式。

function welcome(agent) {
    agent.add(+Getname + `,
    Welcome to my chat bot.`);
}

所以我假设将第一个代码包装在

const getname = "firstcodehere";

但我认为这并不像 iv 多次尝试那样容易。没有运气。必须有一种简单的方法来在多个实例中重用第一个代码,只需一个常量名称。

标签: javascriptnode.jsfirebasegoogle-cloud-firestore

解决方案


事实证明,除了将整个代码包装在此函数中以便它直接返回信息外,几乎没有办法解决这个问题。

那么你可以使用 const "something" = doc.data().name;

所以你可以使用 + something + ' 你的名字对吗?'

感谢所有的帮助。:) 希望这个答案在未来对其他人有所帮助。


推荐阅读