使用Firestore查询信息响应Dialogflow

时间:2018-04-13 21:22:22

标签: javascript node.js google-cloud-firestore dialogflow

尝试通过将数据查询分离到不同的函数然后调用它们来为Dialogflow提供响应文本来使代码更清晰。基本上,我可以调用firestore并撤回列表,但除非我把响应放在.then()中,否则我很困惑如何模块化它。

示例:

render ['alt_index', 'index'].sample

但是,我知道我不能在常规函数上执行.then()...所以有没有办法在不必将所有查询放在主导出的情况下执行此操作?

谢谢!

2 个答案:

答案 0 :(得分:0)

只是

  return getClass;

形成功能。

答案 1 :(得分:0)

你的函数loadClassList()需要返回一个Promise(这是有数据时将调用的.then()方法),而不是实际返回结果。

因此,如果您希望该引用的文档中的数据,您可能会这样做:

function loadClassList() {
  var dbRef = db.collection('playerCharacters');
  return dbRef.get()
    .then( querySnapshot => {
      var docData = querySnapshot.docs.map( docSnapshot => docSnapshot.data() );
      return docData;
    });
};

这将返回一个Promise,最终将解析为playerCharacters集合中的数据。