我刚开始学习Node.js,因此我仍然习惯于异步编程,回调和Promise。我试图从一个记录集中的多个MSSQL查询返回数据,但是我发现的大多数帮助文章都是关于MySQL的。
我尝试按照此处接受的答案中显示的步骤操作:Synchronous database queries with Node.js
在我的SQL函数中,当我打印到控制台时,它正确显示了对象。当我将其退回我的快递路由器并尝试打印该值时,它表示未定义。
这是我的MSSQL函数:
var config = require('../../db/config');
async function getJobData(jobID) {
const sql = require('mssql');
let sqlResult = {};
var lock = 2;
var finishRequest = function() {
// This prints the values properly
console.log(sqlResult['jobData']['recordset']);
console.log(sqlResult['jobText']['recordset']);
return sqlResult;
}
// first query
try {
await sql.connect(config)
let result = await sql.query(`SELECT * FROM Jobs WHERE JobID = ${jobID}`);
lock -= 1;
sqlResult['jobData'] = result;
sql.close();
if (lock === 0) {
finishRequest();
}
} catch (err) {
// ... error checks
console.log(err);
}
// second query
try {
await sql.connect(config)
let result = await sql.query(`SELECT * FROM JDSectionTxt WHERE JobID = ${jobID} ORDER BY TypeID, OrderID`);
lock -= 1;
sqlResult['jobText'] = result;
sql.close();
if (lock === 0) {
finishRequest();
}
} catch (err) {
// ... error checks
console.log(err);
}
}
module.exports = getJobData;
这是我的特快路由器:
const express = require('express');
//....
const app = express();
//....
// Job Descriptions - Edit
app.get('/jds/edit', (req, res) => {
const getJobData = require("../models/jds/getJobData");
let jobID = 0;
if(req.query.jobID){
jobID = parseInt(req.query.jobID);
}
let jobData = getJobData(jobID);
jobData.then(result => {
//This just prints 'undefined'
console.log(result);
res.render('jds/edit', {
data: result
});
}).catch(err => {
console.log(err);
});
})
我需要在此处进行哪些更改以正确地从SQL查询中传递对象,以使结果不不确定?
答案 0 :(得分:1)
我认为您正在使其复杂化。这是应该满足您需求的代码:
var config = require('../../db/config');
const sql = require('mssql');
async function getJobData(jobID) {
let sqlResult = {};
await sql.connect(config)
let firstAwait = getFirstQuery(jobID);
let secondAwait = getSecondQuery(jobID);
sqlResult['jobData'] = await firstAwait;
sqlResult['jobText'] = await secondAwait;
return sqlResult;
}
async function getFirstQuery(jobID) {
try {
return await sql.query(`SELECT * FROM Jobs WHERE JobID = ${jobID}`);
} catch (err) {
// ... error checks
console.log(err);
}
}
async function getSecondQuery(jobID) {
try {
return await sql.query(`SELECT * FROM JDSectionTxt WHERE JobID = ${jobID} ORDER BY TypeID, OrderID`);
} catch (err) {
// ... error checks
console.log(err);
}
}
module.exports = getJobData;
答案 1 :(得分:0)
我认为您错过了退货声明。在第一个和第二个查询try / catch块中,您都应输入return finishRequest()
。
答案 2 :(得分:0)
您尚未将结果返回给函数,因此它以未定义的形式发送它, 您可以像这样更新语句,以查看代码中有什么更新,请查找注释//这是更新的语句
var config = require('../../db/config');
function getJobData(jobID) {
sql = require('mssql');
sqlResult = {};
lock = 2;
var finishRequest = function() {
// This prints the values properly
console.log(sqlResult['jobData']['recordset']);
console.log(sqlResult['jobText']['recordset']);
return sqlResult;
}
try {
await sql.connect(config)
let result = await sql.query(`SELECT * FROM Jobs WHERE JobID = ${jobID}`);
lock -= 1;
sqlResult['jobData'] = result;
sql.close();
if (lock === 0) {
finishRequest();
}
} catch (err) {
// ... error checks
console.log(err);
}
// second query
try {
await sql.connect(config)
let result = await sql.query(`SELECT * FROM JDSectionTxt WHERE JobID = ${jobID} ORDER BY TypeID, OrderID`);
lock -= 1;
sqlResult['jobText'] = result;
sql.close();
if (lock === 0) {
finishRequest();
}
} catch (err) {
// ... error checks
console.log(err);
}
// this is updated statement
return sqlResult;
}
module.exports = getJobData;
答案 3 :(得分:0)
尽管我对async / await不太熟悉,但我相信波纹管代码应该可以工作。
var config = require('../../db/config');
async function getJobData(jobID) {
const sql = require('mssql');
let sqlResult = {};
try {
await sql.connect(config)
sqlResult['jobData'] = await sql.query(`SELECT * FROM Jobs WHERE JobID = ${jobID}`);
} catch (err) {}
try {
sqlResult['jobText'] = await sql.query(`SELECT * FROM JDSectionTxt WHERE JobID = ${jobID} ORDER BY TypeID, OrderID`);
sql.close();
} catch (err) {}
return sqlResult;
}
module.exports = getJobData;
您不需要锁,因为当您使用 await 时,代码实际上是同步的。如果您使用回调,则将需要锁。