Javascript 从mssql db向我的视图传递多个值

Javascript 从mssql db向我的视图传递多个值,javascript,node.js,express,Javascript,Node.js,Express,我目前有一个工作代码来查询我的数据库,并将值传递给view in节点 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: res

我目前有一个工作代码来查询我的数据库,并将值传递给view in节点

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个表并将所有这些值传递给我的视图吗?

您可以使用节点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)
  }
})
要同时运行承诺,请使用:


每个查询都返回一个承诺。要同时运行all,可以使用
Promise.all()
,当它们全部返回时,将触发响应。例如:

sql.connect(config)
    .then(() => {
        const projectPromise = sql.query`select Project_Type_Desc from Project_Type`
        const otherTablePromise = ...
        const anotherTablePromise = ...
        return Promise.all(
            projectPromise,
            otherTablePromise,
            anotherTablePromise
        )
    })
    .then(([projectResult, otherResult, anotherResult]) =>
        res.render('newProject', {projects: result})
    )

我尝试了第一种方法,但得到了以下错误:你知道它可能是什么吗?im使用mssql节点模块连接mssql服务器
sql.connect(config)
    .then(() => {
        const projectPromise = sql.query`select Project_Type_Desc from Project_Type`
        const otherTablePromise = ...
        const anotherTablePromise = ...
        return Promise.all(
            projectPromise,
            otherTablePromise,
            anotherTablePromise
        )
    })
    .then(([projectResult, otherResult, anotherResult]) =>
        res.render('newProject', {projects: result})
    )