函数的返回值在 SqlliteexecuteSql 查询 javascript 中显示未定义
大家
当我学习Javascript时,
我如何从“SELECT ...”语句返回的行中捕获值并将其返回到变量(这里是“端口”),以便我可以在不同的函数中使用该值。
这是我已经被困扰超过 1.5 天的事情了。
这是我写的代码,
function DBQuery(Port_Id)
{
var portid=Port_Id;
var port="";
db.transaction(function (tx){
tx.executeSql('SELECT port_name FROM vact_port where port_id='+portid+'', [], function(tx, results){
var lenPort=results.rows.length;
if(lenPort == 0)
{
alert("No ports found") ;
return ;
}
port = results.rows.item(0).port_name;
alert ("Inside Success" + port) ; //This shows correctly
//return port;
});
alert ("Inside Success" + port) ; //This shows "undefined"
});
}
谢谢 苏纳斯拉
Everyone
While I am learning Javascript,
How i can capture a value from a row returned by a "SELECT ..." statement and return it to variable(here it is 'port') so that i can use that value in different function.
This is something I have been stumped with for over 1.5 days now.
Here is the code I have written,
function DBQuery(Port_Id)
{
var portid=Port_Id;
var port="";
db.transaction(function (tx){
tx.executeSql('SELECT port_name FROM vact_port where port_id='+portid+'', [], function(tx, results){
var lenPort=results.rows.length;
if(lenPort == 0)
{
alert("No ports found") ;
return ;
}
port = results.rows.item(0).port_name;
alert ("Inside Success" + port) ; //This shows correctly
//return port;
});
alert ("Inside Success" + port) ; //This shows "undefined"
});
}
Thanks,
Sunasra
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
alert()
调用位于回调之外。db.transaction() 似乎采用匿名函数作为回调,并使用事务对象调用它。 只要数据库有时间就会发生该调用。对 db.transaction() 的调用安排稍后调用匿名函数。安排该呼叫后,您可以使用尚未设置的
端口
来呼叫警报。您需要在回调中完成所有工作。
Your
alert()
call is outside your callback.db.transaction()
appears to take an anonymous function as a callback, and call it with the transaction object. That call will happen whenever the database gets around to it. The call todb.transaction()
schedules a call to the anonymous function to happen later. After you have scheduled that call, you call alert, usingport
, which has not been set yet.You need to do all your work inside the callback.