简体   繁体   中英

Retrieve data from Firestore to display in DialogFlow

I am trying to get data from firestore and display it in DialogFlow response. When i trigger "buyerBusiness" intent in DialogFlow, the function web fulfillment is successful but the data retrieved does not display as response. It comes in logs when i console them. Below is the handler function written for the same.

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`);
    }

Here are logs for cloud functions -

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

For the Dialogflow fulfillment library to keep track of your async request, you need to return the promise you got from calling firestore.

Try adding return before the admin.firestore().collection call:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM