firestore获取字段以起作用

时间:2018-10-07 05:54:42

标签: javascript node.js firebase google-cloud-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尝试多次那样容易。没有运气。 必须有一种简单的方法,只需一个常量名称即可在多个实例中重用第一个代码。

1 个答案:

答案 0 :(得分:0)

因此,解决这个问题的方法很少,只能将整个代码包装在此函数中,以便直接返回信息。

然后您可以使用const“ something” = doc.data()。name;

这样您就可以使用+某物+'您的名字正确吗? '

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