获取postgres-query的结果作为Node.js中的变量 [英] Get the result of postgres-query as variable in nodejs

查看:110
本文介绍了获取postgres-query的结果作为Node.js中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道以下代码在命令行上显示查询的结果:

I know that the following code prints the result of my query on the command line:

query.on("row", function (row, result) {
  result.addRow(row);
});

query.on("end", function (result, callback) {
    println(JSON.stringify(result))
});

但是,我想在以下代码中将结果值用作变量,而不仅仅是使用它打印到命令行。

However, I want to use the result value in the following code as a variable and not just have it printed out to the command line.

更新:仅能使用下一个查询的值,我希望它不在查询范围之内:

Update: Being able to use the values for the next query is not enough, I want it to be outside of the query scope:

query.on("row", function (row, result) {
  result.addRow(row);
});

query.on("end", function (result, callback) {
    println(JSON.stringify(result))
});
//use the result here

在此先感谢您的回答。

推荐答案

您有2个选择:


  • 如果要继续使用基本驱动程序,则必须使用结果回调,然后将其嵌套以便在下一个查询中使用结果:

  • If you want to continue using the basic driver, you will have to use the result callback, and then nest those in order to use result in the next query:

connection.query(query1, values1, function(err1, result1) {
    // check if(err1) first, then:
    connection.query(query2, values2, function(err2, result2) {
        // check if(err2) first, then:
        connection.query(query3, values3, function(err3, result3) {
            // check if(err3) first, then:
            cb(result1, result2, result3);        
        });
    });
});


  • 另一种方法是使用promises( pg-promise ),当您可以像这样链接呼叫时:

  • The alternative approach is to use promises (pg-promise) when you can chain calls like this:

    db.query(query1, values2)
        .then(data => {
            return db.query(query2, values2);
        })
        .then(data => {
            return db.query(query3, values3);
        })
        .catch(error => {});
    


  • 尽管 pg-promise 在这种情况下是通过方法 task

    Although the right approach for pg-promise in this case is via method task:

    db.task(t => {
        const result = {};
        return t.query(query1, values1)
            .then(data => {
                result.a = data;
                return t.query(query2, values2);
            })
            .then(data => {
                result.b = data;
                return t.query(query3, values3);
            });
            .then(data => {
                result.c = data;
                return result;
            });
    })
        .than(data => {
            // data = {a, b, c}
        })
        .catch(error => {});
    

    第二种方法可以让您在执行多个查询时自动访问ES6和ES7的现代语法。时间,您可以这样做:

    The second approach automatically gives you access to the modern syntax of ES6 and ES7 when executing multiple queries at a time, as you can do:

    对于ES6:

    db.task(function * (t) {
        let a = yield t.query(query1, values1);
        let b = yield t.query(query2, values2);
        let c = yield t.query(query3, values3);
        return {a, b, c};
    })
        .than(data => {
            // data = {a, b, c}
        })
        .catch(error => {});
    

    对于ES7 / Babel:

    For ES7 / Babel:

    db.task(async t => {
        let a = await t.query(query1, values1);
        let b = await t.query(query2, values2);
        let c = await t.query(query3, values3);
        return {a, b, c};
    })
        .than(data => {
            // data = {a, b, c}
        })
        .catch(error => {});
    

    这篇关于获取postgres-query的结果作为Node.js中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆