Dialogflow实现TypeError:无法读取属性“ Parameters” [英] Dialogflow Fulfillment TypeError : Cannot read property 'Parameters'

查看:66
本文介绍了Dialogflow实现TypeError:无法读取属性“ Parameters”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我试图对我的mysql数据库进行网络连接,并且一切正常,直到我尝试添加参数为止。

Hello I'm trying to make a webhook to my mysql database and everything worked perfectly, until I tried to add parameters.

我总是在firebase cloudfunctions

I always get this error in the firebase cloudfunctions


TypeError:无法在exports.dialogflowFirebaseFulfillment.functions.https.onRequest上读取未定义
的属性参数 user_code / index.js:17:47)
在cloudFunction(/user_code/node_modules/firebase-functions/lib/providers/https.js:57:9)
在/ var / tmp / worker / worker.js:783:7
在/var/tmp/worker/worker.js:766:11
在_combinedTickCallback(内部/进程/next_tick.js:73:7)
在process._tickDomainCallback(internal / process / next_tick.js:128:9)

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
这是我的代码:

it's pointing to index.js:17 Here is my code:

// 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行有关:

It means it has something to do with line 17 which is:


const参数= request.body.queryResult.parameters;

const parameters = request.body.queryResult.parameters;

但是我不明白我在做什么错

But I don't understand what I'm doing wrong

非常感谢您的帮助或建议

Help or advice would be much appreciated

推荐答案

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

From your comments, it sounds like you're using Dialogflow v1, which has the parameters at request.body.result.parameters. With Dialogflow v2, this has changed to request.body.queryResult.parameters.

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

If you are still using Dialogflow v1, you should change immediately, since it will no longer be supported soon. To do this, go into your settings in https://console.dialogflow.com/ and make sure you have the v2 API set.

< img src = https://i.stack.imgur.com/T4fV4.png alt =在此处输入图片描述>

这篇关于Dialogflow实现TypeError:无法读取属性“ Parameters”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆