简体   繁体   中英

TypeError: Cannot read property 'then' of undefined Dialogflow Promise

I'm using Dialogflow InLine Editor, and I'm trying to get results from external API, but I'm getting following error on Firebase logs:

TypeError: Cannot read property 'then' of undefined at video (/user_code/index.js:48:9) at WebhookClient.handleRequest (/user_code/node_modules/dialogflow-fulfillment/src/dialogflow-fulfillment.js:303:44) at exports.dialogflowFirebaseFulfillment.functions.https.onRequest (/user_code/index.js:78:9) at cloudFunction (/user_code/node_modules/firebase-functions/lib/providers/https.js:57:9) at /var/tmp/worker/worker.js:725:7 at /var/tmp/worker/worker.js:708:11 at _combinedTickCallback (internal/process/next_tick.js:73:7) at process._tickDomainCallback (internal/process/next_tick.js:128:9)

Here is my code:

'use strict';

const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
const request = require('request-promise-native');

process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements

exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
  const agent = new WebhookClient({ request, response });
  console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
  console.log('Dialogflow Request body: ' + JSON.stringify(request.body));

  function welcome(agent) {
    agent.add(`Welcome to my agent!`);
  }

  function fallback(agent) {
    agent.add(`I didn't understand`);
    agent.add(`I'm sorry, can you try again?`);
}

  function video(agent) {
    agent.add(`Sure, You can access to external API`);
    const url = "https://reqres.in/api/users?page=2";
    return request.get(url)
        .then(jsonBody => {
            var body = JSON.parse(jsonBody);
            agent.add(body.data[0].first_name)
            return Promise.resolve(agent);
        })
        .catch(err => {
            console.error('Problem making network call', err);
            agent.add('Unable to get result');
            return Promise.resolve(agent);
        });
    }

  // Run the proper function handler based on the matched Dialogflow intent name
  let intentMap = new Map();
  intentMap.set('Default Welcome Intent', welcome);
  intentMap.set('Default Fallback Intent', fallback);
  intentMap.set('video', video);
  // intentMap.set('your intent name here', googleAssistantHandler);
  agent.handleRequest(intentMap);
});

EDIT :

I added in index.js :

`const request = require('request-promise-native');`

and in package.json :

"request-promise-native": "^1.0.5",
"request": "^2.88"

Thanks for your help.

return request.get(url)

Request is already defined as a request object and in this case it will try to get a request property.

If you want to fetch something, you would have to use something else like request-promise. https://www.npmjs.com/package/request-promise

The solution was

Add in index.js :

`const rp = require('request-promise-native');`

and in package.json :

"request-promise-native": "^1.0.5",
"request": "^2.88"

And change request to rp . As follows:

return rp.get(url)

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