【发布时间】:2019-04-21 01:10:27
【问题描述】:
我已经编写了一些代码来调用一个过程,它的 OUT 参数类型是游标(ResultSet),所以我必须从 ResultSet 中获取数据,为此我编写了一个从 ResultSet 中提取数据的函数(fetchRowsFromRS()) .
- 我在 fetchRowsFromRS() 中使用了 return 语句,但没有返回 任何东西,变得不确定。
- 当我调用 fetchRowsFromRS() 时,控制不会暂停执行 下一行代码(我使用过 Async/await)这是必需的,因为我想在下一行中使用提取的数据。
我的代码有什么错误?
db.js
connection.execute(plsql,bindvars,options,async function (err, result) {
if (err) {
console.log(err);
console.error(err.message);
doRelease(connection);
return;
}
if(result.outBinds.OUT_STATUS=='SUCCESS'){
if(result.outBinds.OUT_MENU_NAME.metaData.length=0){
loginRes.getUserLoginServiceRes.Header.Status=aes.encryption('Failure');
loginRes.getUserLoginServiceRes.Header.Status_Desc=aes.encryption('No record found in database');
}else{
loginRes.getUserLoginServiceRes.Header.Status=aes.encryption('Success');
loginRes.getUserLoginServiceRes.Header.Status_Desc=aes.encryption('User
logged in successfully');
var numRows=20;
//calling function to fetch data from ResultSet
var rsData=await fetchRowsFromRS(connection,result.outBinds.OUT_MENU_NAME,numRows)
console.log('----------'+rsData);//giving undefined
//here i want to use ResultSet Data
}
}
})
从 ResultSet 中提取数据的函数(不返回任何内容)
function fetchRowsFromRS(connection, resultSet, numRows) {
resultSet.getRows(numRows,function (err, rows) {
if (err) {
console.error(err);
doClose(connection, resultSet); // always close the ResultSet
} else if (rows.length > 0) {
console.log("fetchRowsFromRS(): Got " + rows.length + " rows");
console.log(rows); //getting data here
if (rows.length === numRows) // might be more rows
fetchRowsFromRS(connection, resultSet, numRows);
else
doClose(connection, resultSet); // always close the ResultSet
} else { // no rows
doClose(connection, resultSet); // always close the ResultSet
}
return rows;
});
}
【问题讨论】:
-
“我在
fetchRowsFromRS中使用了return语句”:不,你没有。您在回调函数中有一个return,这是一个不同的函数。 -
什么是
connection.execute?鉴于它需要一个节点样式的回调,它似乎没有承诺感知。不要将async function作为回调传递。 -
有这么多的例子和教程,你为什么不花时间学习javascript的基础知识。您没有使用 async-await,只是要求其他人解决您的问题。
-
@NAVIN 对不起,我正在学习理解javascript和node.js
标签: javascript node.js async-await