javascript - 呈现多个 NodeJs 查询的问题
问题描述
我正在尝试使用 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()
like的回调中移动您的逻辑
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
推荐阅读
- tensorflow - tf.contrib.layers.flatten(x) 张量流的替代函数
- python - Python 实现 KNN 方法
- r - 在 R 中为多个变量创建聚合数据透视表
- java - 为什么 java.util.Set 不能从 java.lang.Iterable 分配?
- docker - Docker 容器内的 GC 统计信息
- python - 测试数据的负 R 方,而训练数据的 R 方很好
- c++ - 错误:对成员的请求不明确 C++;
- node.js - 错误断言 `args[3]->IsInt32()' 失败
- php - 如何设置和获取txt文件的文件属性
- react-native - 如何在 React-Native 中将屏幕从 App.js 导航到第一个屏幕