【问题标题】:How to get results from MySql DB using and send them back to API.ai如何使用 MySql DB 获取结果并将其发送回 API.ai
【发布时间】:2018-06-08 04:17:52
【问题描述】:

在通过 api.ai webhook 使用 SQL 查询并连接到 Google 云 Mysql 数据库时,我需要一些帮助来确定 SQL 查询的语法。 尽管查询正在运行,但“请求已超时”

 'use strict';
const mysql = require('mysql');

exports.name = (req, res) => { 

    let action = req.body.result['action'];

if (action === 'apple') {


    callDB().then((output) => {

        res.setHeader('Content-Type', 'application/json');
        res.send(JSON.stringify(output));
    }).catch((error) => {

        res.setHeader('Content-Type', 'application/json');
        res.send(JSON.stringify(error));
    });

}
};

function callDB() {
    return new Promise((resolve, reject) => {

    try {

        var connection = mysql.createConnection({
            host: "<host>",
            user: "<user>",
            password: "<pass>",
            database: "<DB>"
        });

        connection.query("SELECT description AS solution FROM mtable WHERE title LIKE '%Breakfast%'", function (error, results, fields) {
            if (!error) {

                let response = "The result is: " + results[0].solution;
                response = response.toString();
                let output = {'speech': response, 'displayText': response};
                console.log(output);
                resolve(output);

            } else {

                let output = {'speech': 'Error. Query Failed.', 'displayText': 'Error. Query Failed4.'};
                console.log(output);
                reject(output);

            }
        });
        connection.end();

    } catch (err) {
        let output = {'speech': 'try-catch block error', 'displayText': 'try-catch block error3'};
        console.log(output);
        reject(output);

    }

}
);
}

如果我用这个代替查询,它可以工作:

'SELECT description AS solution FROM mtable WHERE id LIKE 1001'

id 是只有 id 的列名

title 是一个列名,带有诸如Breakfast wrap等标题。

这是 webhook json 上显示的错误的一部分:

"metadata": {
  "intentId": "<id>",
  "webhookUsed": "true",
  "webhookForSlotFillingUsed": "false",
  "webhookResponseTime": 5000,

 "status": {
 "code": 206,
 "errorType": "partial_content",
 "errorDetails": "Webhook call failed. Error: Request timeout.",
 "webhookTimedOut": true
  },

我引用了以下线程的代码, How to get results from MySql DB using node.js MySQL and send them back to API.ai

【问题讨论】:

    标签: javascript mysql dialogflow-es


    【解决方案1】:

    您的查询字符串声明中似乎有错字('%Breakfast%'' 附近):

    connection.query('SELECT description AS solution FROM mtable WHERE title LIKE '%Breakfast%'', function (error, results, fields) {
    

    将查询字符串分配给变量时,'SELECT description AS solution FROM mtable WHERE title LIKE '%Breakfast%'' 被解释为数字(因为 % 运算符)。

    修复单引号有什么帮助吗?

    connection.query("SELECT description AS solution FROM mtable WHERE title LIKE '%Breakfast%'", function (error, results, fields) {
    

    【讨论】:

    • 问候,这确实解决了错误,但是我仍然没有从数据库中获取任何数据。 json只是说超时。 (API.ai 在超时之前只等待 5 秒。可能是什么解决方案?“status”:{“code”:206,“errorType”:“partial_content”,“errorDetails”:“Webhook 调用失败。错误:请求超时。", "webhookTimedOut": true
    • 我对 API.ai 不熟悉。我只发现你的 JavaScript 代码有些奇怪。如果它解决了您在问题中提到的第一个问题,我建议您使用有关当前问题的信息(带有错误消息)更新您的问题,最重要的是,为所涉及的每种技术(api.ai 等)添加一个标签。
    【解决方案2】:

    问题必须在您的服务器端,使用 mysql。我每天都这样做,它可以在 5 秒内使用 mysql 进行查询。

    可能是您的 where 子句正在创建全表扫描,因此超时回到 Diagflow(>5 秒)或数据库连接中断。

    您需要在例程和结束周期之前设置计时器,查看您的持续时间。单独从 bash 脚本运行查询,看看需要多长时间。您会发现超时发生的位置。在条件的两边都有搜索参数 (%) 肯定会比仅搜索字符串开始(意味着搜索开始)与 %search%(查找包含的任何子字符串)花费更长的时间。

    祝你好运。

    【讨论】:

      猜你喜欢
      • 2018-02-06
      • 1970-01-01
      • 2021-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多