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
最满意答案
由于您在此处使用动态生成的控件,为了访问它们或它们的值,您必须在页面回发时重新创建它们。 这是因为动态控件在视图上呈现后会丢失其状态,并且在回发之后再次使用它们的唯一方法是在代码隐藏中重新创建它们。
所以在这里,您只需要使用您第一次创建它的代码重新创建表。
您可以将该代码放在
Page_Load
的if(!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 yourPage_Load
and this will get you going with this.Hope this helps.
相关问答
更多-
下面是在PageLoad期间在ASP.NET中构建动态表,并通过回发读取值的示例。 由于表是动态的,因此当页面回发到服务器时不会重建它。 如果要重建表,则需要再次呈现它并使用从Request.Form中提取的值来重新填充它。 HTML标记您可以循环遍历所有控件并仅拉出TextBoxes: protected void Page_Load(object sender, EventArgs e) { List
allTextBoxes = new List (); FindTextBoxes(this, allTextBoxes); foreach (RadTextBox tb in allTextBoxes) { // Do something h ... 由于您在此处使用动态生成的控件,为了访问它们或它们的值,您必须在页面回发时重新创建它们。 这是因为动态控件在视图上呈现后会丢失其状态,并且在回发之后再次使用它们的唯一方法是在代码隐藏中重新创建它们。 所以在这里,您只需要使用您第一次创建它的代码重新创建表。 您可以将该代码放在Page_Load的if(!IsPostBack)代码块之外,这样您就可以使用它了。 希望这可以帮助。 Since you are using dynamically generated controls here, for you t ...TextBox txt = new TextBox(); txt.ID = "textBox1"; txt.Text = "helloo"; form1.Controls.Add(txt); Label lbl = new Label(); lbl.Text = "I am a label"; form1.Controls.Add(lbl); TextBox txt = new TextBox(); txt.ID = "textBox1"; txt.Text = "helloo"; form1.Cont ...在asp:Repeater中动态提供Id给文本框(服务器端控件)(Dynamically give Id to textbox (Server side control) in asp:Repeater)[2023-04-23]
这对我有效。 请注意,我从转发器中删除了EnableViewState并添加了OnItemDatabound事件。