Search code examples
javascriptparse-platformparse-server

Calling class methods in parse query result block in Javascript


sorry for my english, this is my first question here. I'm having trouble calling the method updateItems from the parse query result block. Both methods belong to the same class. When I perform the search the first console log is the only one that appears in the console. Any class method that I try to call in the success block isn't doing anything.

handleIngredientQuery(text) {
   var Ingredient = Parse.Object.extend('Ingredient');
   var query = new Parse.Query(Ingredient);
   query.startsWith('name', text.toLowerCase());

   query.find({
     success: function (results) {
       console.log('Found ' + results.length);
       this.updateItems(results);
       console.log('finish success callback');
   },

    error: function (error) {
      alert('Error: ' + error.code + ' ' + error.message);
   },
  });
}

updateItems(results) {
  console.log('update items called');
  this.setState({ items: results }, () => {
    console.log('callback');
    console.log(this.state);
  });
}

Solution

  • this is in a different scope for your success function. To get this to behave the way you're expecting, use "fat arrow" syntax like so:

       query.find({
         success: (results) => {
           console.log('Found ' + results.length);
           this.updateItems(results);
           console.log('finish success callback');
       },