Javascript Watson对话-Oracle数据库集成
早上好/下午好,我正在尝试让Watson返回从Oracle数据库手动设置的响应 我使用async顺序访问数据库并返回响应,因为我遇到的第一个问题是,在Watson返回响应之前,DB查询不会发生。瀑布解决了这个问题 我当前的问题是:在控制台中,我看到所有记录都正确。数据库被查询,response.output.text被正确设置,然后返回给Watson,但我的聊天中从未出现任何响应。。如果我在异步之前设置response.output.text=“asdfa”,asdfa将按预期返回 我在试图弄明白这一点时不知所措,所以我感谢所有的帮助。如果需要更多信息,请告诉我Javascript Watson对话-Oracle数据库集成,javascript,node.js,api,watson-conversation,watson,Javascript,Node.js,Api,Watson Conversation,Watson,早上好/下午好,我正在尝试让Watson返回从Oracle数据库手动设置的响应 我使用async顺序访问数据库并返回响应,因为我遇到的第一个问题是,在Watson返回响应之前,DB查询不会发生。瀑布解决了这个问题 我当前的问题是:在控制台中,我看到所有记录都正确。数据库被查询,response.output.text被正确设置,然后返回给Watson,但我的聊天中从未出现任何响应。。如果我在异步之前设置response.output.text=“asdfa”,asdfa将按预期返回 我在试图弄明
// Send the input to the conversation service
conversation.message(payload, function (err, data) {
if (err) {
return res.status(err.code || 500).json(err);
}
return res.json(updateMessage(payload, data));
});
});
function updateMessage(input, response) {
var responseText = null;
if (!response.output) {
response.output = {};
} else {
// checkNames check
if (response.output.nodes_visited[0] === 'slot_11_1519333387192' && response.entities[0].entity === 'confirm') {
/* This code actually returns asdfa as a response from Watson.. */
// response.output.text = "asdfa";
// return response;
async.waterfall([
// this function queries the database
// TODO: module out the Oracle connection parts once POC is completed
function queryDB(callback) {
console.log('Starting queryDB');
var query = "SELECT column_name FROM table@prod WHERE column_id = '" + response.context.VSUID + "'";
oracledb.getConnection('hr',
function (err, connection) {
var conn = oracleGetConnection(err, connection);
conn.execute(query, {}, {
outFormat: oracledb.OBJECT
},
function (err, result) {
console.log('result from Oracle: ', result);
// pass a null error and the result of the query
callback(null, result.rows[0]);
});
});
},
// this function formats the result of the query
// TODO: this should not be it's own function. This can happen at the same time the db gets the row
function formatName (arg1, callback) {
console.log('this should happen after query..');
console.log('arg1: ', arg1);
var r = JSON.stringify(arg1);
r = r.substring((r.indexOf(':') + 1) + 1, r.length - 2);
console.log('Name is: ', r);
// pass a null error and the formatted name
callback(null, r);
}
],
// Final function to be ran after the two above have completed
function finalFunction (err, result) {
if (err) {
console.log('uh oh async err: ', err);
} else {
console.log('This is final Function');
// set output text
response.output.text = 'Is your name ' + result + '?';
// response.context.dbResponse = 'Is your name ' + result + '?';
// response.output.text = "asdfasdfasd";
// console.log('This is the value of response\n\n', response);
// var resp = returnResponse(input, response);
response.context.dbResponse = response.output.text[0];
return returnResponse(input, response);
// return response;
}
});
// response.output.text = "asdfa";
console.log('This is response.output.text ', response.output.text);
return response;
} else {
//If no special if case to query the db just run Watson Conversation stock
return returnResponse(input, response);
}
}
}
下面是一个控制台日志示例
This logs the Input from the user:
name 111111111
This logs the Response from Watson:
Is 111111111correct?
This logs the intent recognized, if any:
nameCheck
This logs the entity recognized, if any:
VSUID
This logs the text that is being returned to the user: [ 'Is 111111111correct?'
]
Starting queryDB
Connected to database
result from Oracle: { outBinds: undefined,
rowsAffected: undefined,
metaData: [ { name: 'TABLE_FIRST_NAME' } ],
rows: [ [ 'Tyler' ], [ 'Tyler' ] ],
resultSet: undefined }
this should happen after query..
arg1: [ 'Tyler' ]
Name is: "Tyler
This is final Function
This logs the Input from the user:
yes
This logs the Response from Watson:
Is your name "Tyler?
This logs the entity recognized, if any:
confirm
This logs the text that is being returned to the user: Is your name "Tyler?
编写如下代码将使您面临SQL注入漏洞(以及可能的性能问题): 请仔细阅读 关于你的问题 您将
updateMessage
视为一个同步函数,但它正在执行异步工作。执行异步工作的函数将需要异步API,例如Node.js样式的回调、承诺或异步函数(async/await)
如果您看到您提供的代码的第73行,那么您正在“返回”响应对象,但这不在async.fallet
调用的范围之内。由于Node.js的异步特性,即使是第67行的返回也无法工作
下面是我最近一次尝试描述这一切是如何运作的:
您可以在此处访问幻灯片和示例代码:
在示例代码的code>headerdetail目录中,您将看到5个不同的文件,它们以headerdetail开头,后面跟着-,但您可以选择不同的API名称。您必须对updateMessage
API做出类似的选择
要运行测试,请使用ddl.sql文件创建目标表,然后根据环境需要编辑db config.js,最后从该目录中的终端运行
node test.js 1
。您可以更改结尾处的数字以运行不同的测试文件。Dan,您真是太棒了。我已经观看了您的视频(顺便说一句,您将其删除),并使用示例代码重建了我的异步调用。遗憾的是,我遇到了同样的问题,因为我的数据库代码依赖于Watson的“response”对象,无论我如何实现它,似乎在异步调用完成之前就返回了响应。这是当前的代码。我想从逻辑上知道我应该如何处理这件事。。谢谢看看这是否有帮助:我不能测试它的语法错误等,但也许它会给你的想法…经过一些代码的改编后,我有它的工作丹!!你给我的帮助比我得到的任何支持都多,这太疯狂了。非常感谢。非常感谢。非常感谢。很高兴听到这个消息!
var query = "SELECT column_name FROM table@prod WHERE column_id = '" + response.context.VSUID + "'";