1 回答
TA贡献1866条经验 获得超5个赞
async据我了解,您已经使用and包装了导出的函数await:
module.exports = function(id) {
return new Promise((resolve, reject) => {
const pool = require('../loaders/pool.js')();
pool.query(
"SELECT * FROM dealerships WHERE id = '" + id + "';",
(err, res) => {
if (err) {
console.error(err.stack);
reject(err);
}
else {
resolve(res.rows[0]);
}
}
);
});
};
所以现在只需在调用代码中使用asyncand :await
async function getData() {
const dealership = await require('../models/getDealershipById.js')(id);
const dealershipLeads = await require('../models/getLeadsByDealershipId.js')(dealership.id);
const dealershipStaff = await require('../models/getStaffByDealershipId.js')(dealership.id);
}
getData()
重要提示:作为旁注,请避免"SELECT * FROM dealerships WHERE id = '" + id + "';",因为这是一个真正糟糕的安全反模式,如果id未正确清理,它很容易允许 SQL 代码注入。pool.query("SELECT * FROM dealerships WHERE id = ?", [id], ...)如果您的驱动程序允许,请使用类似的东西,或者将其替换为允许的驱动程序或包装器。
添加回答
举报