Функция продолжает падать из-за ошибки «Невозможно прочитать свойство» добавить «неопределенного» - PullRequest
0 голосов
/ 07 января 2020

Я получаю эту ошибку, когда запускаю эту функцию Firebase:

TypeError: Cannot read property 'add' of undefined
    at visaCountry (/srv/index.js:51:11)
    at exports.dialogflowFirebaseFulfillment.functions.https.onRequest (/srv/index.js:105:40)
    at cloudFunction (/srv/node_modules/firebase-functions/lib/providers/https.js:57:9)
    at /worker/worker.js:783:7
    at /worker/worker.js:766:11
    at _combinedTickCallback (internal/process/next_tick.js:132:7)
    at process._tickDomainCallback (internal/process/next_tick.js:219:9)

Переменная agent работает в заданной области действия, но возвращает undefined, когда внутри функции visaCountry , Кто-нибудь знает, почему это происходит?

Функция

// See https://github.com/dialogflow/dialogflow-fulfillment-nodejs
// for Dialogflow fulfillment library docs, samples, and to report issues
'use strict';

const functions = require('firebase-functions');
const admin = require('firebase-admin');

const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');

var serviceAccount = require("./config/sdkjfhdsjkhfjdsf.json");

admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    databaseURL: "https://sdfdsfjshdfjds.firebaseio.com"
});

var db = admin.firestore();

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

exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
  const agent = new WebhookClient({ request, response });

  console.log('Agent = '+ agent);

  console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
  console.log('Dialogflow Request body: ' + JSON.stringify(request.body));

  // An action is a string used to identify what needs to be done in fulfillment
  let action = agent.action;
  console.log('Actions = '+ JSON.stringify(action));

  // Parameters are any entites that Dialogflow has extracted from the request.
  const parameters = agent.parameters; // https://dialogflow.com/docs/actions-and-parameters

  // Contexts are objects used to track and store conversation state
  const inputContexts = agent.contexts;

  function visaCountry(agent) {
    let visasRead = db.collection('requirements').doc('India').get();

    console.log('Agent = '+ agent);

    agent.add(`Here are the visas for` + visasRead.name);
  }
  let intentMap = new Map();
  intentMap.set('Default Fallback Intent', fallback);
  intentMap.set('Visa Search Request', visaCountry());
  // intentMap.set('your intent name here', googleAssistantHandler);
  agent.handleRequest(intentMap);
});

1 Ответ

1 голос
/ 07 января 2020

Причина этого в том, что вы определили свою функцию как function visaCountry(agent) { }. Технически, пытаясь получить доступ к переменной agent из области действия функции, которую вы не передали, и поэтому она является undefined.

Просто просто передайте visaCountry() переменной agent.

Например:

intentMap.set('Visa Search Request', visaCountry(agent));

Представьте себе следующее:

const agent = 'data';

run();
run(agent);

function run(agent) {
  console.log(agent);
}

Надеюсь, это поможет!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...