将数组从javascript类方法返回到脚本 [英] Returning array from javascript class method to a script

查看:100
本文介绍了将数组从javascript类方法返回到脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用面向对象的技术构建一个JavaScript应用程序,我遇到一个问题,希望有人能帮助我解决。被设计为从Web SQL数据库返回填充有数据行的数组:

  retrieveAllStoreSearches:function(){
this.db.transaction(
function(transaction){
transaction.executeSql(
SELECT name,store,address FROM searchResults ORDER BY name ASC,
[],
函数(事务,结果){
var returnArr = [];
for(var i = 0; i< results.rows.length; i ++){
var row = results.rows.item(i);
returnArr.push(row.name +'|'+ row.address);
}
console.log('Length of returnArr:' + returnArr.length);
的console.log(returnArr);
return returnArr;
},
this.errorHandler
);
}
);
}

当将结果记录到控制台时,这一切与预期一样工作但是当我尝试在下面的代码片段中调用该方法(位于不同的脚本中 - 初始化所有对象并负责构建应用程序的DOM结构和功能)

 的console.log(db.retrieveAllStoreSearches()); 

未定义返回。



我可以不知道我做错了什么,当我使用return方法,允许一个对象被访问从一个类和一个不同的脚本我从来没有遇到任何问题。



任何人都可以提供任何可能出错的指针?

解决方案

无法完成,如果您的功能正在调用异步函数,返回结果的唯一方法是通过回调。这就是异步功能的整体,其余的代码可以在调用完成之前继续进行。这是一个不同的方式来考虑返回值(而不会阻止其余的代码)。



所以你必须将代码更改为以下(加上正确的错误处理)

  retrieveAllStoreSearches:function(callback){
this.db.transaction(
function ){
transaction.executeSql(
SELECT name,store,address FROM searchResults ORDER BY name ASC,
[],
function(transaction,results){
var returnArr = [];
for(var i = 0; i< results.rows.length; i ++){
var row = results.rows.item(i);
returnArr.push(row.name +'|'+ row.address);
}
回调(returnArr);
},
this.errorHandler
);
}
);
}

然后你可以使用 console.log 像以下

  db.retrieveAllStoreSearches(function(records){
console.log(records)
});


I'm building a javascript application using object oriented techniques and I'm running into a problem that I hope someone here can help me resolve.

The following method is designed to return an array populated with rows of data from a web SQL database:

retrieveAllStoreSearches : function(){
    this.db.transaction(
        function(transaction){
            transaction.executeSql(
                "SELECT name,store,address FROM searchResults ORDER BY name ASC",
                [],
                function(transaction, results){
                    var returnArr = [];
                    for(var i = 0; i < results.rows.length; i++){
                        var row = results.rows.item(i);
                        returnArr.push(row.name + ' | ' + row.address);
                    }
                    console.log('Length of returnArr: ' + returnArr.length);
                    console.log(returnArr);
                    return returnArr;
                },
                this.errorHandler
            );  
        }
    );
}

This works exactly as expected when logging the results to the console BUT when I try to call the method in the following snippet (located in a different script - which initialises all objects and is responsible for building the application DOM structure and functionality)

console.log(db.retrieveAllStoreSearches());

undefined is returned.

I can't figure out what I am doing wrong as when I have used return in a method to allow an object to be accessed from one class and into a different script I have never encountered any problems.

Could anyone provide any pointers on what I might be doing wrong?

解决方案

Cannot be done, if your function is calling an asynchronous function, the only way to return results is through a callback. That's the whole point of asynchronous functions, the rest of the code can keep going before the call is finished. It's a different way of thinking about returning values (without blocking the rest of your code).

So you'd have to change your code to the following (plus proper error handling)

retrieveAllStoreSearches : function(callback){
    this.db.transaction(
        function(transaction){
            transaction.executeSql(
                "SELECT name,store,address FROM searchResults ORDER BY name ASC",
                [],
                function(transaction, results){
                    var returnArr = [];
                    for(var i = 0; i < results.rows.length; i++){
                        var row = results.rows.item(i);
                        returnArr.push(row.name + ' | ' + row.address);
                    }
                    callback( returnArr );
                },
                this.errorHandler
            );
        }
    );
}

Then you can use console.log like the following

db.retrieveAllStoreSearches(function(records) {
    console.log(records ) 
});

这篇关于将数组从javascript类方法返回到脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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