-2

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

Inzamam Malik
  • 3,238
  • 3
  • 29
  • 61

1 Answers1

0

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"
      }));
    });
});
Prisoner
  • 49,922
  • 7
  • 53
  • 105
  • Did you tried this code, I have already tried exec approach it was not working for me, well i will try it again will let you know – Inzamam Malik Jun 12 '18 at 06:38
  • In that document they heve clearly mentioned "Queries are not promises" – Inzamam Malik Jun 12 '18 at 06:52
  • As I said, I have not. I don't use Mongoose. Correct, Queries are not Promises. But, as the code shows, `query.exec()` returns a Promise when done this way. – Prisoner Jun 12 '18 at 10:03
  • Then please update your original question which shows how you are using the code I illustrated and what "is not working". – Prisoner Jun 19 '18 at 15:44