[英]Problems rendering multiple NodeJs queries
我正在嘗試使用 Nodejs 在視圖中呈現幾個查詢,但控制台返回 Promise {pending }。
請幫忙,我不知道我做錯了什么。
router.get('/edit/:id', function (req, res, next) {
const{id} = req.params;
const linksUpdate = db.query('SELECT L.id,L.title,L.url,L.description,U.fullname,U.id FROM links AS L INNER JOIN users AS U ON L.user_id=U.id WHERE L.id = ?'[id], (error, results, fields) => {
if (error) throw error;
const users = db.query("SELECT U.id, U.username, U.password, U.fullname FROM users AS U", (error, resp,
fields) => { if (error) throw error;
console.log(linksUpdate);
console.log(users);
res.render('links/edit', {linksUpdate: linksUpdate,users: users
});
});
});
});
您需要在db.query()
的回調中移動邏輯,例如
router.get('/edit/:id', function(req, res, next) {
const { id } = req.params;
db.query('SELECT L.id,L.title,L.url,L.description,U.fullname,U.id FROM links AS L INNER JOIN users AS U ON L.user_id=U.id WHERE L.id = ?' [id], (error, linksUpdate) => {
if (error) return next(err);
db.query("SELECT U.id, U.username, U.password, U.fullname FROM users AS U", (error, users) => {
if (error) return next(err);
console.log(linksUpdate);
console.log(users);
res.render('links/edit', {
linksUpdate: linksUpdate,
users: users
});
});
});
});
或等待返回的承諾,而不是使用回調,如此處詳細說明:https://codeburst.io/node-js-mysql-and-async-await-6fb25b01b628
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.