无法获取变量中的数组值-角度

时间:2018-10-11 06:56:54

标签: javascript angular ionic-framework ionic2 ionic3

简要说明:我已经使用res从变量**js**中获取了结果。

控制台上res的结果如下所示。

see here

  

要求:我想获取角度变量中的res的值。我已经宣布

resarry = [];

当我这样做

this.resarry = res;
console.log(this.resaary);

错误即将发生-无法设置“ resarray”的属性未定义。

console.log(results); // no problem in this line console.log(this.resarry); // giving error

export class HomePage {
resarry = [];


constructor(){
     var connection = new JsStore.Instance();
              var dbName = 'Demo';
          connection.openDb(dbName);

          connection.select({
            from: Test1,
          }).then(function(res) {
         // results will be array of objects

            console.log(res,'results');
            this.resarry = results;
console.log(results); // no problem in this line
console.log(this.resarry); // giving error

          }).catch(function(err) {
            console.log(err, 'error');
            alert(err.message);
        });

}
}

5 个答案:

答案 0 :(得分:2)

更改:

connection.select({
  from: Test1,
}).then(function(res) {
  // ...
});

收件人:

connection.select({
  from: Test1,
}).then(res => {
  // ...
});

基本上,function() { ... }不能从外部范围访问this,而箭头功能可以。 here可以找到更深入的解释。

还有arrow functions' docs

答案 1 :(得分:0)

resarry:number[] = new Array();

将初始化一个未定义的空数组。

您可以设置预期结果的类型。

答案 2 :(得分:0)

connection.select({
         from: Test1,
      }).then(function(res) { // results will be array of objects
        console.log(res,'results');
        this.resarry = results; //  perhaps like that => this.resarry = res
        console.log(results); // i think, you should have an error on this line because **results** isn't a variable but a string in your console
        console.log(this.resarry); // giving error

      })

答案 3 :(得分:0)

因为,“ this”是当前功能对象。因此,您在构造函数中使用的“ this”不是实际的组件

使用箭头功能或

constructor(){
     var connection = new JsStore.Instance();
              var dbName = 'Demo';
          connection.openDb(dbName);
     var $this = this;
          connection.select({
            from: Test1,
          }).then(function(res) {
         // results will be array of objects

            console.log(res,'results');
            $this.resarry = results;
console.log(results); // no problem in this line
console.log($this.resarry); // giving error

          }).catch(function(err) {
            console.log(err, 'error');
            alert(err.message);
        });

}

答案 4 :(得分:0)

用户箭头功能以及打字稿类型可在分配前验证内容

export class HomePage {
    resarry: any[] = []; //--> resarry: any[] -> used to set the type as array
    constructor() {
        let connection = new JsStore.Instance(); //-> use Let insted of Var
        let dbName = 'Demo';
        connection.openDb(dbName);
        connection.select({
            from: "Test1",
        }).then(res => {
            console.log(res);
            if (res)
                if (res.length > 0){
                    this.resarry = res;
                    console.log(this.resarry);
                    console.log("connection Successful with array objects");
                }else{
                    console.log("connection Successful  without array objects");
                }
        }), err => {
            console.log("connection error");
        };

    }
}