2017-12-27 224 views
0

我需要一些幫助來確定SQL查詢的語法,同時通過連接到Google雲Mysql數據庫的api.ai webhook &使用它們。 雖然查詢工作,在 '請求被超時'如何從MySql數據庫中獲取結果並將它們發送回API.ai

'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描述AS解決方案從mtable WHERE ID LIKE 1001'

ID是列名,只有id`s

標題列名和標題,如早餐包等

這是網絡掛接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

回答

0

似乎是在您的查詢字符串聲明中的錯別字(接近「%早餐% ''):

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

在分配您的查詢字符串變量, 'SELECT描述AS解決方案從mtable WHERE標題LIKE '%早餐%'' 被解釋爲一個數字(因爲%運營商)。

以任何方式修復您的單引號是否有幫助?

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

問候,這確實解決了錯誤,但我仍然沒有從數據庫中獲取任何數據。 json只是說超時。 (API.ai在超時之前僅等待5秒,可能是什麼解決方案?「狀態」:{ 「code」:206, 「errorType」:「partial_content」, 「errorDetails」:「Webhook調用失敗。錯誤:請求超時。「, 」webhookTimedOut「:true – Jarvis

+0

我對API.ai並不熟悉我只在您的JavaScript代碼中發現了一些奇怪的東西 如果它解決了您在問題中提到的第一個問題,您的問題與關於您當前問題(包含錯誤消息)和最重要的信息相關,爲每項涉及的技術(api.ai等)添加標籤。 – RogerC

0

問題必須在您的服務器端與MySQL。我每天都這樣做,並且在5秒鐘內用於查詢的情況下,它可以正常工作。

可能是你的Where子句正在創建全表掃描,因此超時返回Diagflow(> 5秒)或db連接中斷。

您需要在例程和結束週期之前設置定時器,查看您的持續時間。單獨從bash腳本運行查詢並查看需要多長時間。你會發現超時正在發生的地方。在條件兩邊都有搜索參數(%)肯定會比搜索字符串的開始時間(意味着以搜索開始)和%search%(找到包含的任何子字符串)花費更長的時間。

祝你好運。

相關問題