从Firestore检索数据以在DialogFlow中显示

时间:2018-06-12 10:08:26

标签: node.js firebase-realtime-database google-cloud-firestore bluebird dialogflow

我正在尝试从firestore获取数据并在DialogFlow响应中显示它。当我在DialogFlow中触发“buyerBusiness”意图时,功能Web实现成功,但检索到的数据不显示为响应。当我安装它时,它会出现在日志中。下面是为此编写的处理函数。

function buyerBusiness(agent) {
        console.log("buyer name is " + params.BuyerEntity);
        var abc = new Set();
        var results = [];
        admin.firestore().collection('SuppBuyAssoc').where('Advertiser', '==', params.BuyerEntity).limit(5).get().then(snapshot => {
            snapshot.forEach(doc => {
                abc.add(doc.data().Agency);
            });
            var x = Array.from(abc);
            var y;
            for (var j = 0; j < x.length; j++) {
                y = results.push(x[j]);
            }

            blueBird.all(results).then(function () {
                console.log('results are: ' + results);
                agent.add(results);
            }).catch(eror => {
                console.log("Errrrr");
                console.log(eror);
            })
            console.log("results are:"+ results);

        }).catch(reason => {
            // res.send(reason)
            console.log(reason);
        })

        // agent.add(`Testing this stuff`);
    }

以下是云功能的日志 -

3:32:33.069 PM info dialogflowFirebaseFulfillment results are: Publicis,Ogilvy,GroupM,WPP
3:32:32.668 PM info dialogflowFirebaseFulfillment results are:Publicis,Ogilvy,GroupM,WPP
3:31:55.910 PM
outlined_flag   
dialogflowFirebaseFulfillment
Function execution took 2930 ms, finished with status code: 200
3:31:54.253 PM
info    
dialogflowFirebaseFulfillment
buyer name is IBM
3:31:54.253 PM
info    
dialogflowFirebaseFulfillment
Dialogflow Request body: ----------
3:31:54.247 PM
info    
dialogflowFirebaseFulfillment
Dialogflow Request headers: ---------
3:31:52.981 PM
outlined_flag   
dialogflowFirebaseFulfillment
-------
3:31:52.981 PM
outlined_flag   
dialogflowFirebaseFulfillment
Function execution started

1 个答案:

答案 0 :(得分:1)

要使Dialogflow履行库跟踪您的异步请求,您需要返回通过调用firestore获得的承诺。

尝试在admin.firestore()。集合调用之前添加return:

function buyerBusiness(agent) {
  console.log("buyer name is " + params.BuyerEntity);
  var abc = new Set();
  var results = [];
  return admin.firestore().collection(/* rest of code */