1

I have following code

export class AppComponent implements OnInit {
  public guests: any[] = [];


  constructor(private apiService: ApiService) {

  }

  ngOnInit(): void {
    this.apiService.getAllGuestsQuery().find({
      success(results) {
        for (let i = 0; i < results.length; i++) {
          const object = results[i];
          console.log(object.get('companyName'));
        }
      },
      error(error) {
        console.log("Error: " + error.code + " " + error.message);
      }
    });
  }
}

The getAllGuestsQuery returns a Parse.Query object. I would like to store the result in my guests array. E.g. do something like this.quests = results But I cannot access this.guests class variable inside the success method.

How can I do that?

0

2 Answers 2

1
export class AppComponent implements OnInit {
  public guests: any[] = [];


  constructor(private apiService: ApiService) {

  }

  ngOnInit(): void {
    let vm = this;
    this.apiService.getAllGuestsQuery().find({
        func(r){ vm.guests } // will be available
    });
  }
}

This is caused because when you pass the {} construct to the find() function a new scope is being created there. If you used the ES6 arrow syntax you could make this better(but a hassle for me to write, you'd have to handle the errors in a totally different manner, preferably promises)

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

Comments

0

You could try passing a reference to this in a separate variable at the beginning of your ngOnInit() method. Something like let that = this;. Refer to this (class instance) via that later on in your callback.

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.