【问题标题】:Cannot fetch array value in variable - angular无法获取变量中的数组值 - 角度
【发布时间】:2019-03-16 04:21:28
【问题描述】:

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

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);
        });

}
}

【问题讨论】:

  • 如果你使用function(res)这个范围是丢失使用箭头函数(res) => {} 你不会丢失
  • 试试resarry: any;constructor(){ this.resarry={}}
  • console.log(this.resaary) 或 console.log(this.resarray);; ?

标签: javascript angular ionic-framework ionic2 ionic3


【解决方案1】:
resarry:number[] = new Array();

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

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

【讨论】:

  • 这没有用,这是一个焦点问题
【解决方案2】:

变化:

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

到:

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

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

另外,arrow functions' docs

【讨论】:

  • 好吧,但是需要更多描述才能知道这个焦点留在了 function(){} 而不是 () => {}
  • @mtizziani 对,我添加了最少的解释。我觉得官方文档很清楚,但添加它的要点不会有坏处。
【解决方案3】:
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

      })

【讨论】:

  • 请按照我在代码中添加的 cmets 进行操作
【解决方案4】:

因为,“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);
        });

}

【讨论】:

    【解决方案5】:

    用户箭头函数以及打字稿类型在分配之前验证事物

    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");
            };
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-28
      • 1970-01-01
      相关资源
      最近更新 更多