gpt4 book ai didi

javascript - 将多个值从 mssql db 传递到我的 View

转载 作者:行者123 更新时间:2023-11-30 11:33:12 28 4
gpt4 key购买 nike

我目前有一个工作代码来查询我的数据库并将值传递给 Node 中的 View 。

router.get('/', function(req, res, next) {
sql.connect(config).then(() => {
return sql.query`select Project_Type_Desc from Project_Type`;
}).then(result => {
res.render('newProject', {projects: result});
}).catch(err => {
console.log(err);
})
});

但是有人可以告诉我如何查询另外 4 个表并将所有这些值传递到我的 View 吗?

最佳答案

你可以做一个Promise链使用async/await在 Node v7+ 中:

router.get('/', async (req, res, next) => {
await sql.connect(config)

try {
const projects = await sql.query(`select Project_Type_Desc from Project_Type`)
const result2 = await sql.query(`another query`)
const result2 = await sql.query(`another query`)
const result4 = await sql.query(`another query`)

res.render('newProject', {
projects,
result2,
result3,
result4
})
} catch (error) {
console.log(error)
}
})

要同时运行 Promises,请使用 Promise.all :

router.get('/', async (req, res, next) => {
await sql.connect(config)

const promises = Promise.all([
await sql.query(`select Project_Type_Desc from Project_Type`),
const result2 = await sql.query(`another query`),
const result2 = await sql.query(`another query`),
const result4 = await sql.query(`another query`)
])

try {
const [projects, result2, result3, result4] = await promises

res.render('newProject', {
projects,
result2,
result3,
result4
})
} catch (error) {
console.log(error)
}
})

关于javascript - 将多个值从 mssql db 传递到我的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45492804/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com