自从星期四以来,我一直困扰于此问题,我不知道出了什么问题。在从数据库中检索数据之前,查找日志似乎已完成HTTP请求。我尝试了每种与async / await的组合...
你们知道如何解决这个问题吗?
app.get("/getData", (req, res) => {
let data = oracledb.getConnection(
{
user: "hr",
password: "hr",
connectString: "localhost/user"
},
(err, connection) => {
if (err) {
console.error(err.message);
return;
}
queries.getData(connection, 3);
}
);
console.log('Arrives here:', data)
res.send(data)
})
const getData = (conn, id) => {
const query = `SELECT * FROM SoapDataObject WHERE id = ${id}`;
let result = conn.execute(query, (err, result)=> {
if (err) {
console.error(err.message);
doRelease(conn);
return;
}
// console.log(result.metaData);
console.log(result.rows);
doRelease(conn);
return result.rows
})
return result;
};
控制台显示此内容
REST API server started on: 3000
[1] Listening on port 8001
[1] WSDL available at http://localhost:8001/soapWs?wsdl
[1] Arrives here: undefined
[1] [ [ 3, '1010', 11, 11, 11, 11, 11, 2 ] ]
答案 0 :(得分:0)
问题就这么简单了,
let data = oracledb.getConnection(
{
user: "hr",
password: "hr",
connectString: "localhost/user"
},
(err, connection) => {
if (err) {
console.error(err.message);
return;
}
queries.getData(connection, 3);
}
);
console.log('Arrives here:', data)
res.send(data)
请注意,直到您return
都不会返回getData的任何结果。另外,在您的这种方法中:
const getData = (conn, id) => {
const query = `SELECT * FROM SoapDataObject WHERE id = ${id}`;
let result = conn.execute(query, (err, result)=> {
if (err) {
console.error(err.message);
doRelease(conn);
return;
}
// console.log(result.metaData);
console.log(result.rows);
doRelease(conn);
return result.rows
})
return result;
};
请注意,您不能像在同步编程中那样从回调中返回对象。而是在这两种方法中,您都试图返回无法正常工作的回调方法的执行结果。您必须在回调范围内捕获结果,然后传播结果。 (Johnathan提出的一种更优雅的解决方案是使调用SQL语句的方法繁多,以便您可以返回'promises'然后获取值。(请记住,async / await只能与Promise和NOT回调)。
一个示例实现因此成为(使用promise):
app.get("/getData", (req, res) => {
const dataPromise = new Promise((resolve, reject) => {
oracledb.getConnection(
{
user: "hr",
password: "hr",
connectString: "localhost/user"
},
(err, connection) => {
if (err) {
reject(error);
return;
}
queries.getData(connection, 3).then((response) => {
resolve(response)
}).catch(qerror => {
reject(qerror);
})
}
)
});
dataPromise.then((data) => {
res.send(data);
}).catch(error => {
// Your error handling here
})
})
const getData = (conn, id) => {
return new Promise((resolve, reject) => {
const query = `SELECT * FROM SoapDataObject WHERE id = ${id}`;
conn.execute(query, (err, result) => {
if (err) {
doRelease(conn);
reject(err);
return;
}
doRelease(conn);
resolve(result.rows);
})
});
};
或者,如果您希望坚持使用回调:
app.get("/getData", (req, res) => {
oracledb.getConnection(
{
user: "hr",
password: "hr",
connectString: "localhost/user"
},
(err, connection) => {
if (err) {
res.status(500).send(error);
return;
}
const id = 3;
const query = `SELECT * FROM SoapDataObject WHERE id = ${id}`;
connection.execute(query, (qerr, result) => {
if (qerr) {
doRelease(connection);
// Replace 500 with code of your choice
res.status(500).send(qerr);
return;
}
doRelease(connection);
res.status(200).send(result.rows);
return;
})
}
);
});