我正在尝试使用nodejs restapi构建一个json响应,但是即使该函数尚未执行,我的响应也总是较早发送。
我试图保证没有任何作用并发送错误。
//这是我想要的json结构
[
{
"user_id": "2",
"user_name": "Mehedi Hasan",
"permissions": [
{
"table_name": "questionbank",
"permission_create": "true",
"permission_update": "true",
"permission_delete": "true"
},
{
"table_name": "questionbankquestions",
"permission_create": "true",
"permission_update": "true",
"permission_delete": "true"
},
{
"table_name": "subjects",
"permission_create": "false",
"permission_update": "true",
"permission_delete": "false"
},
{
"table_name": "subjectquestions",
"permission_create": "true",
"permission_update": "true",
"permission_delete": "true"
},
{
"table_name": "modeltests",
"permission_create": "true",
"permission_update": "true",
"permission_delete": "true"
},
{
"table_name": "modeltestquestions",
"permission_create": "true",
"permission_update": "true",
"permission_delete": "true"
}
]
},
............................
]
//这是我尝试过的
function getAllUser(done) {
var sql = "SELECT users.id, users.username FROM users";
db.query(sql, [], function(err, rows, fields) {
if (err) throw err;
done(rows);
});
}
/*--------------------------Check user permission --------------------------*/
router.get("/adminpermissions", function(req, res) {
var sql =
"SELECT table_name, permission_create, permission_update, permission_delete FROM permissions " +
"WHERE user_id = ?";
finalArray = [];
jsonFormat = [];
permissionArray = [];
getAllUser(function(user) {
for (var i = 0; i < user.length; i++) {
userId = user[i].id;
userName = user[i].username;
db.query(sql, [userId], function(err, rows, fields) {
if (err) {
res.status(500).send({ error: "Something failed!" });
}
console.log(rows);
permissionArray[i] = rows;
var data = {
user_id: userId,
user_name: userName,
permission: permissionArray[i]
};
jsonFormat.push(data);
});
}
res.json(jsonFormat);
});
});
我总是以[]数组作为响应..尽管我知道即使循环尚未完成但仍在发送响应的问题,但不知道如何解决..试图用promise等待,但最终还是这样许多错误消息。
答案 0 :(得分:1)
您可以使用async / await解决问题。您可以执行以下操作:-
router.get("/adminpermissions", function(req, res) {
var sql =
"SELECT table_name, permission_create, permission_update, permission_delete FROM
permissions " +
"WHERE user_id = ?";
finalArray = [];
jsonFormat = [];
permissionArray = [];
getAllUser(async function(user) {
for (var i = 0; i < user.length; i++) {
userId = user[i].id;
userName = user[i].username;
sql = "SELECT table_name, permission_create, permission_update, permission_delete
FROM permissions WHERE user_id = "+ userId;
try {
let result = await db.query(sql)
permissionArray[i] = result[0];
var data = {
user_id: userId,
user_name: userName,
permission: permissionArray[i]
};
jsonFormat.push(data);
} catch(err) {
res.status(500).send({ error: "Something failed!" });
}
}
res.json(jsonFormat);
}); });
答案 1 :(得分:0)
这就是我所做的,效果很好
/*------------------------ Get Permissions of All User -----------------------------*/
function getAllUser(done) {
var sql = "SELECT users.id, users.username FROM users";
db.query(sql, [], function(err, rows, fields) {
if (err) throw err;
done(rows);
});
}
function getPermission(user_id, user_name,profile_image, done) {
var sql =
"SELECT table_name, permission_create, permission_update, permission_delete FROM permissions " +
"WHERE user_id =?";
db.query(sql, [user_id], function(err, rows, fields) {
if (err) throw err;
done(user_id, user_name, profile_image, rows);
});
}
router.get("/allpermissions", function(req, res) {
var i = 0;
var data = [];
getAllUser(function(user) {
for (i = 0; i < user.length; i++) {
getPermission(user[i].id, user[i].username,user[i].profile_image, function(
userId,
userName,
profileImage,
rows
) {
data.push({
user_id: userId,
user_name: userName,
profile_image: profileImage,
permissions: rows
});
if (data.length == user.length) {
res.json(data);
}
//console.log(data);
});
}
});
});
/*------------------------ ***************************** ---------------------------*/