简体   繁体   English

Node.js布尔变量不更改值

[英]Node.js boolean variable not changing value

I have following code: 我有以下代码:

app.post('/', function (req, res)
    {
        /*
        connection is created with mysql
         */
        var success = true
        connection.query(sql, [values], function (err, result)
        {
            success = false
        })
        connection.end()
        console.log(success)
    }
)

The output of this is true . 此输出为true How do I change the value of success from inside of connection.query? 如何从connection.query内部更改成功的值?

This is Async program, your console.log() statement is getting executed before the connection.query callback function. 这是异步程序,您的console.log()语句在connection.query回调函数之前得到执行。 The Value of success is being altered inside the function. 成功的价值正在功能内部被改变。

You can test this by: 您可以通过以下方式对此进行测试:

app.post('/', function (req, res) {
    var success = true;
    connection.query(sql, [values], function (err, result){
       success = false;
       console.log(success);
       // more code goes here
    });
    connection.end();
});

If you want to write mode code, that'll go inside the callback function, where the comment exist. 如果要编写模式代码,它将放在存在注释的回调函数中。

Welcome to Node.js ;) 欢迎使用Node.js;)

Though the code block in which success is redefined success = false; 虽然其中代码块success被重新定义success = false; is "before" console.log(result) in terms of line numbers. 就行号而言,是console.log(result)之前的。 It actually takes place after. 它实际上发生在之后。 connection.query() is invoked with a function which will get invoked at a later time, after the query has completed. 用一个函数调用connection.query() ,该函数将在查询完成后在以后的时间被调用。

There's at least 3 different ways to handle this. 至少有3种不同的方式来处理此问题。

the callback style this is simply wrapping the code you want to execute in the conn.query anonymous function like so 回调样式,这只是将要执行的代码包装在conn.query匿名函数中,如下所示

app.post('/', function (req, res)
    {
        /*
        connection is created with mysql
        */
        var success = true;
        var callback = function(result) {
            success = result;
            console.log(success);
            connection.end()
        };

        connection.query(sql, [values], function (err, result)
        {
            callback(result);
        });
    }
);

the promise style if conn.query() returns returns a promise then you could do something like this: 如果conn.query()返回的诺言样式返回一个诺言,则可以执行以下操作:

app.post('/', function (req, res)
    {
        /*
        connection is created with mysql
        */
        var success = true;
        var callback = function(result) {
            success = result;
            console.log(success);
            connection.end()
        };

        connection.query(sql, [values]).then(callback);
    }
);

more info on Promises 有关承诺的更多信息

Also, if you're using the mysql driver for node, you can also use promise-mysql to use promises. 另外,如果您将mysql驱动程序用于节点,则也可以使用promise-mysql来使用promises。

the await style which operate more intuitively for those who aren't used to event-driven programming. 等待样式 ,对于不习惯事件驱动的编程的人来说,操作更直观。 Assuming your conn.query() supports Promises, you could use await and it would look something like this: 假设您的conn.query()支持Promises,则可以使用await ,它看起来像这样:

app.post('/', function (req, res)
    {
        /*
        connection is created with mysql
        */
        var success = true;
        await connection.query(sql, [values], (result) => success = false);
        console.log(success);
        connection.end();
    }
);

more info on await 等待更多信息

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

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