【问题标题】:Unable to get desired result with Promise in Dialogflow API V2 DB Call在 Dialogflow API V2 DB 调用中无法使用 Promise 获得所需的结果
【发布时间】:2018-08-06 14:23:34
【问题描述】:

我正在尝试连接到 MySQL 数据库,获取结果并将其传递给代理以显示给用户。由于 DB Query 是异步的,我使用 Promise 来获取回调并在解析数据后发送响应。我确实在日志中得到响应,但函数执行在此之前结束,它似乎没有等待 then 调用。

代码如下:

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


  function welcome(agent) {
    agent.add(`Welcome to my agent!`);
  }

  function fallback(agent) {
    agent.add(`I didn't understand`);
    agent.add(`I'm sorry, can you try again?`);
}

    function getBalance(){

                sqlQuery = 'select * from master_balance where account_no = \'1234567\'';

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

                var balance_type = request.body.queryResult.parameters['balance_type'];
                if(balance_type == 'SMS'){
                    console.log('In SMS');
                    reply = 'You have '+ results[0].bal_sms +' SMS left in your account';
                }
                else if(balance_type == 'Voice'){
                    console.log('In Voice-- from DB--'+results[0].bal_call);
                    reply = 'Your Voice balance is $ '+results[0].bal_call;
                }
                else if(balance_type == 'Data'){
                    console.log('In Data');
                    reply = 'You have '+results[0].bal_data +' MB left in your account';
                }
                else{
                    console.log('In Ahh');
                    reply = 'Ahh, there seems to be some issue. Please wait';
                }
                agent.add(reply);
                 return 1;
                 }).catch((error) => {
                     console.log('in catch----'+error);

                    });
        return 1;
     }



  let intentMap = new Map();
  intentMap.set('Default Welcome Intent', welcome);
  intentMap.set('Default Fallback Intent', fallback);
  intentMap.set('Query.Balance', getBalance);

  agent.handleRequest(intentMap);
});

function callDB() {
        return new Promise((resolve, reject) => {
        console.log('-- In callDB--'+sqlQuery);
        try {
            var connection = mysql.createConnection({
                socketPath: '/cloudsql/' + connectionName,
                user: dbUser,
                password: dbPass,
                database: dbName
            });
            connection.query(sqlQuery, function (error, results, fields) {
                if (!error) {

                    console.log('--In no error 2--'+results[0]);
                    resolve(results);

                } else {

                    let output = {'speech': 'Error. Query Failed.', 'displayText': 'Error. Query Failed.'};
                    console.log('--- in else----'+error);

                    reject(results);

                }
            });
            connection.end();

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

        }

    }
    );
}

请帮助我解决我可能做错的事情。另一件事是,对于 Node JS,我是个菜鸟。
提前致谢!!

【问题讨论】:

    标签: mysql node.js dialogflow-es


    【解决方案1】:

    问题在于,如果您使用的是异步函数,那么您的意图处理程序也必须返回一个 Promise。将回复作为 then() 子句的一部分发送是不够的,您还必须返回 then() 所属的 Promise。

    在您的情况下,这看起来相当容易。在getBalance() 函数中,您将返回callDB().then().catch() 结果,这是一个Promise。 (then() 和 `catch() 返回一个 Promise)

    return callDB().then((results) => {
      ....
    

    【讨论】:

    • 好吧.. 谢谢。我会试试看,会更新的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-18
    • 1970-01-01
    • 2015-01-10
    • 1970-01-01
    • 2018-12-10
    相关资源
    最近更新 更多