Search code examples
node.jsmongoosees6-promiseactions-on-googledialogflow-es

Dialogflow V2 error - Async call is not working - Promise Error? firebase function


I wanted to get some data from mongodb (using mongoose framework) but unable to get the data,

previously i was using callback method as described here to getting data which was working well for me in "action on google v1" but not working in v2,

then i read here that we must need to use promise in order to make async call in "action on google v2", then i refactored my code according to the instruction of Mr. @prisoner in the question above

you can see my code here:

import * as functions from 'firebase-functions';

import {
    dialogflow, 
    SimpleResponse,
    Suggestions, 
    DialogflowConversation, 
    DialogflowApp
} from 'actions-on-google'

import { Model } from './db'

const app = dialogflow({ debug: false })

app.middleware((conv) => {
    conv["hasScreen"] =
        conv.surface.capabilities.has('actions.capability.SCREEN_OUTPUT');
    conv["hasAudioPlayback"] =
        conv.surface.capabilities.has('actions.capability.AUDIO_OUTPUT');
});

app.intent('Get Some Data', (conv) => {    

        console.log("Get Some Data Intent triggered")

        return new Promise(function (resolve, reject) {

            Model.find({}, (err, result: any) => {
                if (!err) {

                    if (!result.length) {

                        console.log("no data found")
                        conv.ask(new SimpleResponse({
                            speech: "no data found"
                        }))
                        resolve();

                    } else {

                        console.log("lots of data found")
                        conv.ask(new SimpleResponse({
                            speech: "lots of data found"
                        }));
                        resolve();

                    }
                } else {
                    console.log("Error in getting data: ", err);
                    conv.ask(new SimpleResponse({
                        speech: "Error in getting data"
                    }))
                    resolve();
                }
            })
        })
});

exports.webhook = functions.https.onRequest(app);

it is still not working for me, and function is ending with timeout

Actually i have my app up and running properly in v1, and now I'm trying to migrate from v1 to v2 since i need to use some latest features which are not available in v1 like voice authentication and other new features. any help will be warmly welcomed


Solution

  • The page you referenced at the Mongoose documentation that talks about find() also gives information about how to use it with Promises. The find() call returns a Query and you can use query.exec() to get the Promise that you'll then work with.

    So the code might look something like this (untested, since I don't use Mongoose):

    app.intent('Get Some Data', (conv) => {
      var query = Model.find({});
      return query.exec()
    
        .then( result => {
          if( !result || !result.length ){
            return conv.ask(new SimpleResponse({
              speech: "no data found"
            }));
    
          } else {
            return conv.ask(new SimpleResponse({
              speech: "some data found"
            }));
          }
        })
    
        .catch( err => {
          return conv.close(new SimpleResponse({
            speech: "something went wrong"
          }));
        });
    });