繁体   English   中英

如何为 Node.js 返回多个 sql 表

[英]How do I return more than one sql table for Node.js

我正在尝试从一台路由器下的 MySQL 数据库中返回多个表,但是无论我尝试什么,似乎我都无法让它返回多个值,现在如果我更改结果。那么我可以让其中一个表返回,我想让两个表同时返回。

我执行 SQL 的代码

function exec(sql) {
    const promise = new Promise((resolve, reject) => {
        con.query(sql, (err, result) => {
            if (err) {
                reject(err)
                return
            }
            resolve(result)
        })
    })
  return promise
}

我的功能代码:

const getCluster1 = (list) =>{
    let sql = `select * from demoClusterDescription where 1=1 `
    
    return exec(sql)

}
const getCluster2 = (list) =>{
    let sql = `select * from prefClusterDescription where 1=1 `
    
    return exec(sql)

}

我的退货代码

router.get('/demo', function(req,res,next)  {
    
    const { list } = req.query
    const result1 = getCluster1(list)
    const result2 = getCluster2(list)

    
    return result1.then(cluster => {
      res.json(
         new SuccessModel(cluster)
      )
   })
})

省去一些麻烦,放弃老式的.then()语法,转而采用async/await风格:

router.get('/demo', async function(req,res,next)  {
    
    const { list } = req.query
    const result1 = await getCluster1(list)
    const result2 = await getCluster2(list)

    console.log("result1 = ", result1)
    console.log("result2 = ", result2)
    
    // Do something with them

})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM