首页 文章

使用Firestore查询信息响应Dialogflow

提问于
浏览
1

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

例:

exports.app = functions.https.onRequest((request,response) => {
var listOfClasses = loadClassList().then(
//respond to the user
)};



function loadClassList() {
  var dbRef = db.collection('playerCharacters');
  var getClass = dbRef.get()
  .then(
  //do stuff with the queried data
  ) 
};

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

谢谢!

2 回答

  • 0

    只是

    return getClass;
    

    形成功能 .

  • 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 集合中的数据 .

相关问题