Dialogflow Fulfillment TypeError : Cannot read property 'Parameters'

Dialogflow Fulfillment TypeError : Cannot read property 'Parameters'

您好,我正在尝试对我的 mysql 数据库进行网络挂接,并且一切正常,直到我尝试添加参数。

我总是在 firebase 云函数中遇到这个错误

TypeError: Cannot read property 'parameters' of undefined at exports.dialogflowFirebaseFulfillment.functions.https.onRequest (/user_code/index.js:17:47) at cloudFunction (/user_code/node_modules/firebase-functions/lib/providers/https.js:57:9) at /var/tmp/worker/worker.js:783:7 at /var/tmp/worker/worker.js:766:11 at _combinedTickCallback (internal/process/next_tick.js:73:7) at process._tickDomainCallback (internal/process/next_tick.js:128:9)

它指向 index.js:17 这是我的代码:

// 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 {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
const mysql = require('mysql');
const express = require('express');

const bodypaser = require('body-parser');


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

exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
   const parameters = request.body.queryResult.parameters;
   const emailPar = parameters['email'];
   const namePar    = parameters['name'];
  console.log(emailPar);
  console.log(namePar);


  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) {
    console.log('Inside function welcome');

   return callbackDB().then((rows) =>{
        console.log('inside callabck in welcome function');
        var reply =rows[0].ID;
        var name = rows[0].display_name;
        agent.add(`Welcome to my agent! My ID is: 0` + reply);
        agent.add(`My name is `+ name);

   }).catch((error) =>{
        console.log('In catch ERROR::: ' +error); 
   });

  }
  function Orders(agent){
    return getOrderCallback().then((rows) =>{
        console.log('inside getOrderCallabck in orders function');
        var id =rows[0].order_id;
        var firstname = rows[0].billing_first_name;
        var lastname = rows[0].billing_last_name;
        var email = rows[0].billing_email;
        var ordereditems =rows[0].order_items;
        var dateorder =rows[0].post_date;
        var billing_address = rows[0].billing_address_1 + ' Postcode: '+ rows[0].billing_postcode + ' City : ' + rows[0].billing_city + ' Country: ' + rows[0].billing_state;
        agent.add('Hello there! The current orders made by you are the following: ');
        agent.add(`--Current Orders ---------------------------`);
        agent.add(`Order ID:  `+ id);
        agent.add('For : ' + firstname + ' ' + lastname);
        agent.add(`Contact: ` + email);
        agent.add(`Shipping Address: ` + billing_address);
        agent.add(`Order placed on :` + dateorder);
        agent.add(`Ordered Items : ` + ordereditems);

   }).catch((error) =>{
        console.log('In catch ERROR::: ' +error); 
   });
  }
  function fallback(agent) {
    console.log('Inside function fallback');
    agent.add(`I didn't understand`);
    agent.add(`I'm sorry, can you try again?`);
  }


  let intentMap = new Map();
  intentMap.set('Default Welcome Intent', welcome);
  intentMap.set('Default Fallback Intent', fallback);
  intentMap.set('GetOrdersByEmailAndName', Orders);
  // intentMap.set('your intent name here', yourFunctionHandler);
  // intentMap.set('your intent name here', googleAssistantHandler);
  agent.handleRequest(intentMap);

  function getOrderCallback(){
    return new Promise((resolve,reject) =>{
        console.log('Inside getOrderCallback');
      try {
        var mysqlConnection = mysql.createConnection({
            host:'xxxx',
            user:'xxxxxxxxxxxxxxx',
            password:'xxxxxxxxx',
            database:'xxxxxxxxxxxxx',});

        mysqlConnection.connect((err)=>{
            if(!err){
                console.log('DB connection succeeded.');
                console.log('passed parameters : '+ emailPar + ' ' + namePar);
                mysqlConnection.query(`select
                                            p.ID as order_id,
                                            p.post_date,
                                            max( CASE WHEN pm.meta_key = '_billing_email' and p.ID = pm.post_id THEN pm.meta_value END ) as billing_email,
                                            max( CASE WHEN pm.meta_key = '_billing_first_name' and p.ID = pm.post_id THEN pm.meta_value END ) as billing_first_name,
                                            max( CASE WHEN pm.meta_key = '_billing_last_name' and p.ID = pm.post_id THEN pm.meta_value END ) as billing_last_name,
                                            max( CASE WHEN pm.meta_key = '_billing_address_1' and p.ID = pm.post_id THEN pm.meta_value END ) as billing_address_1,
                                            max( CASE WHEN pm.meta_key = '_billing_address_2' and p.ID = pm.post_id THEN pm.meta_value END ) as billing_address_2,
                                            max( CASE WHEN pm.meta_key = '_billing_city' and p.ID = pm.post_id THEN pm.meta_value END ) as billing_city,
                                            max( CASE WHEN pm.meta_key = '_billing_state' and p.ID = pm.post_id THEN pm.meta_value END ) as billing_state,
                                            max( CASE WHEN pm.meta_key = '_billing_postcode' and p.ID = pm.post_id THEN pm.meta_value END ) as billing_postcode,
                                            max( CASE WHEN pm.meta_key = '_order_total' and p.ID = pm.post_id THEN pm.meta_value END ) as order_total,
                                            max( CASE WHEN pm.meta_key = '_order_tax' and p.ID = pm.post_id THEN pm.meta_value END ) as order_tax,
                                            max( CASE WHEN pm.meta_key = '_paid_date' and p.ID = pm.post_id THEN pm.meta_value END ) as paid_date,
                                            ( select group_concat( order_item_name separator '|' ) from wp_woocommerce_order_items where order_id = p.ID ) as order_items
                                        from
                                            wp_posts p 
                                            join wp_postmeta pm on p.ID = pm.post_id
                                            join wp_woocommerce_order_items oi on p.ID = oi.order_id
                                        group by
                                            p.ID
                                        HAVING billing_email = ? AND billing_first_name = ?`,[emailPar,namePar],
                 (error,rows,fields)=>{
                    if(!error){
                        console.log(rows);

                        resolve(rows);
                        }
                    else{
                        console.log(error);
                        reject(rows);
                        }
                    });

            }
        else{
            console.log('DB connection failed.');
        }
  });


      }
      catch (err){
        let results = 'error in try-catch';
        console.log(results);
        reject(results);
      }


    });
}


function callbackDB(){
    return new Promise((resolve,reject) =>{
      console.log('Inside callbackDB');

      try {
        var mysqlConnection = mysql.createConnection({
            host:'xxxx',
            user:'xxxxxxxxxxxxxxx',
            password:'xxxxxxxxx',
            database:'xxxxxxxxxxxxx',});
        mysqlConnection.connect((err)=>{
            if(!err){
                console.log('DB connection succeeded.');
                mysqlConnection.query('SELECT * FROM wp_users',(error,rows,fields)=>{
                    if(!error){
                        console.log(rows);
                        resolve(rows);
                        }
                    else{
                        console.log(error);
                        reject(rows);
                        }
                    });

            }
        else{
            console.log('DB connection failed.');
        }
  });


      }
      catch (err){
        let results = 'error in try-catch';
        console.log(results);
        reject(results);
      }

    });

}
});

这意味着它与第 17 行有关:

const parameters = request.body.queryResult.parameters;

但我不明白我做错了什么

非常感谢帮助或建议

从您的评论来看,您似乎正在使用 Dialogflow v1,其参数位于 request.body.result.parameters。对于 Dialogflow v2,这已更改为 request.body.queryResult.parameters

如果您仍在使用 Dialogflow v1,您应该立即更改,因为它很快将不再受支持。为此,请进入 https://console.dialogflow.com/ 中的设置并确保设置了 v2 API。