1

I'm using Algolia to build a search function. The function successfully retrieves the wanted data, but I'm struggling to read it into an array. Consider the function:

searchQ() {
 this.loading = true ;

 this.index.search({
   query: this.searchQuery
 },
 async function searchDone(err, content) {
    if (err) throw err;
    this.SearchResults = content.hits ;  
    console.log(this.SearchResults) ;
    console.log('hello2');
    this.loading = false ;
  }
 );

 this.searchActive = true ;
 console.log(this.SearchResults) ;
 console.log('hello1');
 this.changeRef.detectChanges();
}

See the attached image of the console output.enter image description here

On a second run of the search function, the same output is shown. The array associated with 'hello1' never takes on any values outside the searchDone function.

Search results declared here:

export class Tab2Page {

  public SearchResults = [] ;
  ...

  constructor(...) {

  }
}
  • 2
    Very likely to be down to [this](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) and/or [this](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron). E.g., that you're trying to use `SearchResults` *before* the async operation assigning it a value has finished. – T.J. Crowder Feb 21 '19 at 13:08

0 Answers0