首页 \ 问答 \ NodeJS,mysql和异步结果[重复](NodeJS, mysql, and async results [duplicate])

NodeJS,mysql和异步结果[重复](NodeJS, mysql, and async results [duplicate])

这个问题在这里已有答案:

我目前正在使用nodeJS截断我的数据库中的60个表的列表。

为此,我使用此代码:

var mysql      = require('mysql');

var connexion = mysql.createConnection({
  host     : '1.1.1.1',
  user     : 'user',
  password : 'password',
  database : 'db'
});
connexion.connect();
var tableList = [
    'table0',
    'table1',
    'table2',
    ...
];
console.log('\n### Truncating tables... ###');
var i;
for(i = 0; i < tableList.length; i++){
    connexion.query('TRUNCATE '+tableList[i]+';', function(err, rows, fields){
        if(err) throw err;
        console.log(i+'===> '+tableList[i]+' truncated.')
    });

}

问题是输出总是:

60===> undefined truncated.
60===> undefined truncated.
60===> undefined truncated.
60===> undefined truncated.
60===> undefined truncated.
60===> undefined truncated.

我有两个问题,tableList数组在回调函数中是不可访问的,并且回调函数一直认为i = 60是正确的值。

我究竟做错了什么?

我希望有类似的东西:

0===> table0 truncated.
1===> table1 truncated.
...

This question already has an answer here:

I'm currently using nodeJS to truncate a list of 60 tables in my database.

To do this, I'm using this code:

var mysql      = require('mysql');

var connexion = mysql.createConnection({
  host     : '1.1.1.1',
  user     : 'user',
  password : 'password',
  database : 'db'
});
connexion.connect();
var tableList = [
    'table0',
    'table1',
    'table2',
    ...
];
console.log('\n### Truncating tables... ###');
var i;
for(i = 0; i < tableList.length; i++){
    connexion.query('TRUNCATE '+tableList[i]+';', function(err, rows, fields){
        if(err) throw err;
        console.log(i+'===> '+tableList[i]+' truncated.')
    });

}

The problem is that the output is always:

60===> undefined truncated.
60===> undefined truncated.
60===> undefined truncated.
60===> undefined truncated.
60===> undefined truncated.
60===> undefined truncated.

I have two problems, the tableList array is not accessible within the callback function, and the callback function keeps thinking i=60 is the correct value.

What am I doing wrong?

I would like to have something like:

0===> table0 truncated.
1===> table1 truncated.
...

原文:https://stackoverflow.com/questions/30566364
更新时间:2023-09-03 18:09

最满意答案

由于您在此处使用动态生成的控件,为了访问它们或它们的值,您必须在页面回发时重新创建它们。 这是因为动态控件在视图上呈现后会丢失其状态,并且在回发之后再次使用它们的唯一方法是在代码隐藏中重新创建它们。

所以在这里,您只需要使用您第一次创建它的代码重新创建表。

您可以将该代码放在Page_Loadif(!IsPostBack)代码块之外,这样您就可以使用它了。

希望这可以帮助。


Since you are using dynamically generated controls here, for you to access them or their values, you will have to recreate them when the page is postback. This is because dynamic controls lose their state after they have been rendered on the view and the only way to play with them again after postback is to re-create them in code-behind.

So here, you just need to recreate the table using the code that you use to create it for the first time.

You can put that code outside the if(!IsPostBack) code block in your Page_Load and this will get you going with this.

Hope this helps.

相关问答

更多