如何在对 NodeJS 中的数据库进行多次异步调用的过程中发送响应?
How to send response in mid of multiple async calls to database in NodeJS?
需要终止 POST
函数并发送 Error: something
作为响应,而不终止程序。
样本for-loop
:
for (let i = 0; i < req.body.listoftouristsbyid.length; i++) {
client.query("SELECT tourists.id FROM tourists WHERE tourists.id = " + req.body.listoftouristsbyid[i], function(err, result) {
done();
if (result.rows.length === 0) {
console.log("Tourist " + req.body.listoftouristsbyid[i] + " you want to add does not exist");
res.status(500);
return res.render("Bad listoftoutistbyid request, student with id " + i + " does not exist");
}
})
}
我应该写什么来代替 return res.render
,这样 POST 就不起作用,函数将终止并返回错误代码作为响应,但不会导致程序崩溃,这样我以后可以发送更多请求?
尝试使用 async.map 来解决您的问题。 for
循环不适用于异步函数。
async.map(
req.body.listoftouristsbyid,
function(touristId, callback) {
client.query("<query>", function(err, result) {
if (result.rows.length === 0) {
callback(new Error("not found"));
}
callback(null, result);
});
},
function(err, result) {
// send response here.
}
);
您可以为此使用 async-await
,因为它在 Promise
和 Generators
.
等异步调用处理程序中被广泛使用
这是一个示例代码:
app.post('/APIendpoint', async (req, res) => {
let listoftouristsbyid = req.body.listoftouristsbyid;
let dbResult = [];
// Can put this whole for loop inside try too. Then success response will also be inside try after for-loop
for (let i = 0; i < listoftouristsbyid.length; i++) {
try {
let result = await callDB( "SELECT tourists.id FROM tourists WHERE tourists.id = " + listoftouristsbyid[i] );
dbResult.push(result);
}
catch (err) {
console.log("Tourist " + listoftouristsbyid[i] + " you want to add does not exist");
res.status(500);
return res.send("Bad listoftoutistbyid request, student with id " + i + " does not exist");
}
}
console.log("Successfully fetched all records ", dbResult);
res.status(200);
return res.send(dbResult);
});
function callDB(query) {
return new Promise ( (resolve, reject) => {
client.query(query, function(err, result) {
if (result.rows.length === 0) {
reject("not found");
} else {
resolve(result.rows);
}
})
});
}
需要终止 POST
函数并发送 Error: something
作为响应,而不终止程序。
样本for-loop
:
for (let i = 0; i < req.body.listoftouristsbyid.length; i++) {
client.query("SELECT tourists.id FROM tourists WHERE tourists.id = " + req.body.listoftouristsbyid[i], function(err, result) {
done();
if (result.rows.length === 0) {
console.log("Tourist " + req.body.listoftouristsbyid[i] + " you want to add does not exist");
res.status(500);
return res.render("Bad listoftoutistbyid request, student with id " + i + " does not exist");
}
})
}
我应该写什么来代替 return res.render
,这样 POST 就不起作用,函数将终止并返回错误代码作为响应,但不会导致程序崩溃,这样我以后可以发送更多请求?
尝试使用 async.map 来解决您的问题。 for
循环不适用于异步函数。
async.map(
req.body.listoftouristsbyid,
function(touristId, callback) {
client.query("<query>", function(err, result) {
if (result.rows.length === 0) {
callback(new Error("not found"));
}
callback(null, result);
});
},
function(err, result) {
// send response here.
}
);
您可以为此使用 async-await
,因为它在 Promise
和 Generators
.
这是一个示例代码:
app.post('/APIendpoint', async (req, res) => {
let listoftouristsbyid = req.body.listoftouristsbyid;
let dbResult = [];
// Can put this whole for loop inside try too. Then success response will also be inside try after for-loop
for (let i = 0; i < listoftouristsbyid.length; i++) {
try {
let result = await callDB( "SELECT tourists.id FROM tourists WHERE tourists.id = " + listoftouristsbyid[i] );
dbResult.push(result);
}
catch (err) {
console.log("Tourist " + listoftouristsbyid[i] + " you want to add does not exist");
res.status(500);
return res.send("Bad listoftoutistbyid request, student with id " + i + " does not exist");
}
}
console.log("Successfully fetched all records ", dbResult);
res.status(200);
return res.send(dbResult);
});
function callDB(query) {
return new Promise ( (resolve, reject) => {
client.query(query, function(err, result) {
if (result.rows.length === 0) {
reject("not found");
} else {
resolve(result.rows);
}
})
});
}