使用mssql模块查询要发送到节点中的多个项目

时间:2017-08-03 17:35:23

标签: javascript node.js express

我正在尝试查询多个表并将这些值传递给我的视图,这是我生成错误的代码。

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

请让我知道我做错了什么。谢谢你的到来。

1 个答案:

答案 0 :(得分:0)

我认为您将第一个查询中的值转换为变量result。但是,然后您再次进行查询并且在保留result范围时不保留该值,您将需要执行此类操作。

router.get('/', function(req, res, next) {
    // I've added a variable here which we will use to save the query
    // result to.
    let projectResult;
    sql.connect(config).then(() => {
        return sql.query`select Project_Type_Desc from Project_Type`
    }).then(result => {
        // Now we save the query result
        projectResult = result;
        return sql.query`select Curency_Code from Currency_List`
    }).then(cur => {
        // We are now outside of the scope from the last block (above), so we use
        // the value we saved in the parent scope (router.get).
        res.render('newProject', {projects: projectResult} , {currency : curr})
    }).catch(err => {
        console.log(err)
    });
});