0

Brief Explanation: I have fetched result in variable res using **js**.

Result of res on console is shown below.

see here

Requirement: I want to get the value of res in angular variable. I have declared

resarry = [];

When i do

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

Error coming - Cannot set property of 'resarray` undefined.

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

}
}
4
  • 1
    if you use function(res) the scope of this is lost use arrow functions (res) => {} and you will not lose Commented Oct 11, 2018 at 7:02
  • try resarry: any; and in constructor(){ this.resarry={}} Commented Oct 11, 2018 at 7:04
  • Possible duplicate of How to access the correct `this` inside a callback? Commented Oct 11, 2018 at 7:08
  • console.log(this.resaary) or console.log(this.resarray);; ? Commented Oct 11, 2018 at 7:09

5 Answers 5

2

Change:

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

to:

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

Basically, function() { ... } can't access this from the outer scope, while arrow functions can. A more in-depth explanation can be found here.

Also, arrow functions' docs.

Sign up to request clarification or add additional context in comments.

2 Comments

all right, but a little more description is necessary to know that focus of this got left in function(){} and not on () => {}
@mtizziani Right, I added minimal explanation. I feel like the official docs was clear enough but adding the gist of it won't hurt.
0
resarry:number[] = new Array();

will initialize an empty array which is not undefined.

you can set the type where your result is expected to be.

1 Comment

this doesn't help, its a focus problem on this
0

Because, the "this" meant to be the current function object. So the "this", you have used in the constructor is not the actual component

Use Arrow function or

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

}

Comments

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

      })

1 Comment

Please, follow the comments that i added in the code
0

User arrow functions as well as typescript types to validate things before assigning

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

    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.