javascript - 我在命令提示符下从 MySQL 获取值。但是这些值如何在聊天机器人中显示呢?

标签 javascript mysql node.js ibm-watson

我正在使用 IbmWatson 创建一个聊天机器人。当用户向我的聊天机器人询问某些产品的成本时,这些值来自 MySQL 服务器。我完成了所有设置并获得了值。但该值显示在命令提示符中,而不是聊天机器人中。

如何在聊天机器人中显示值。

我正在使用 Node.js

我正在尝试这段代码:

  function updateMessage(input, response){

    var responseText = null;
    if (response.intents && response.intents[0]) {

    var intent = response.intents[0];  
    if (intent.intent==='Sales-1'){
    var rsp = con.query("SELECT Mobiles from Salestb", function (err, 
    result, fields) {
    if (err) throw err;
    console.log(result);
    result.forEach( (row) => {
    console.log(`${row.Mobiles}`);

    });

  });


      response.output.text = "This is the modified Output :"+ rsp;       
}  }

我正在尝试上面的代码来获取命令提示符中的值:

[ RowDataPacket { Mobiles: '2,894,564' } ]

2,894,564

在聊天机器人中:这是修改后的输出:[object Object]

值以对象格式显示。但我想要显示值。如何解决这个问题?

最佳答案

您在命令提示符中获得输出,因为 yopu 使用 console.log:

console.log(`${row.Mobiles}`);

您应该将响应更改为:

response.output.text = "This is the modified Output :"+ ${row.Mobiles};  

编辑 像这样将你的查询函数放入

function updateMessage(input, response){

    var responseText = null;
    if (response.intents && response.intents[0]) {

        var intent = response.intents[0];  
        if (intent.intent==='Sales-1'){
            var rsp = con.query("SELECT Mobiles from Salestb", function (err,result, fields) {
                if (err) throw err;
                console.log(result);
                result.forEach( (row) => {
                    console.log(`${row.Mobiles}`);        
                });
                response.output.text = "This is the modified Output :"+ ${row.Mobiles};
            });           
        }  
    }
}

关于javascript - 我在命令提示符下从 MySQL 获取值。但是这些值如何在聊天机器人中显示呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55725771/

相关文章:

javascript - 在 Canvas 上绘制透明背景的图像

javascript - 为什么 Function.toString 会这样工作?

javascript - 代码被困在连续的 for 循环中的某个地方,我不知道为什么

javascript - 如何在模糊主浏览器窗口的同时将图像显示为叠加层?

java - 在多线程环境中选择后更新表

mysql - 仅在 vb.net 中查询同一按钮

mysql - 如何设置指向不同模式中的表的外键约束?

node.js - App 和 Web 中的 VoIP 集成

linux - 为什么 npm 要求输入密码?

javascript - .then()方法返回的promise中 promise 了什么?