简体   繁体   中英

How to display a value from snap on DialogFlow fullfilment using Firestore

I am trying to make an intent to display info that is stored on Firestore.

This is my DB. 在此处输入图片说明

I would like to display "definicion" value.

This is my Intent on Js (usingNodejs):

    app.intent('my.def.intent', (conv) => {
    // Trying to get Data from firestone DB,
        var platformRef = db.collection("plataformas").doc("slack");
        var getDef =  platformRef.get()
        .then( snap =>{
            var dat = "";
            if (snap.exists) {
                dat =  snap.data("definicion");
            }
             return dat;
        })
        .catch( err => {
            console.log("error...", err);
        });
    // This is the response for Actions on Google
    conv.ask(new SimpleResponse({
                  speech:"This is the def: " + getDef,
                  text:"This is the def: " + getDef,
                }));
    });

This code makes a display on the ActionsOnGoogle Simulator something like this:

This is the def: [objecto Promise]

I don't understand whats happenning here . Why i cant display the info from "definiciones" and instead that, there is an [object promise]? how can i display the info?

Thanks!!!

Call to platformRef.get().then( ... ) is asynchronous and returns a Promise, so when you access the value of getDef, its value is a Promise.

To fix it you should put the conv.ask code inside of your .then block above. However, since you already have access to value of snap inside, you won't really need the value of getDef and can use snap directly. Finally, you'll need to return the value of that promise from your intent function. Putting this together gives:

app.intent('my.def.intent', (conv) => {
   // Trying to get Data from firestone DB,
   var platformRef = db.collection("plataformas").doc("slack");
   return platformRef.get()
            .then( snap => {
              var dat = "";
                if (snap.exists) {
                  dat =  snap.data("definicion");
                }
               // This is the response for Actions on Google
               conv.ask(new SimpleResponse({
                  speech:"This is the def: " + dat,
                  text:"This is the def: " + dat,
               }));
           })
           .catch( err => {
             console.log("error...", err);
           });
 });

I suggest taking a look at the official sample - dialogflow-updates-nodejs . It also uses Firestore and can help with coding.

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