TypeError: Невозможно прочитать свойство then для неопределенного обещания Dialogflow - PullRequest
0 голосов
/ 10 января 2019

Я использую Dialogflow InLine Editor и пытаюсь получить результаты от внешнего API, но я получаю следующую ошибку в журналах Firebase:

TypeError: Невозможно прочитать свойство 'then' из неопределенного на видео (/user_code/index.js:48:9) в 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) в cloudFunction (/user_code/node_modules/firebase-functions/lib/providers/https.js:57:9) на /var/tmp/worker/worker.js:725:7 по адресу /var/tmp/worker/worker.js:708:11 в _combinedTickCallback (внутренняя / process / next_tick.js: 73: 7) at process._tickDomainCallback (internal / process / next_tick.js: 128: 9)

Вот мой код:

'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 :

Я добавил в index.js :

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

и package.json :

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

Спасибо за вашу помощь.

Ответы [ 2 ]

0 голосов
/ 10 января 2019

Решение было

Добавить в index.js :

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

и в package.json :

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

И изменить запрос на rp . Следующим образом:

return rp.get(url)
0 голосов
/ 10 января 2019
return request.get(url)

Запрос уже определен как объект запроса, и в этом случае он попытается получить свойство запроса.

Если вы хотите получить что-то, вам придется использовать что-то еще, например запрос-обещание. https://www.npmjs.com/package/request-promise

...