Julien Breem
Julien Breem

Reputation: 113

Unable to assign result from subscription to referenced array

I hope I didn't commit any rookie mistakes, but I can't seem to figure this one. So here's the code:

this is a function inside a component

search(term: string,service,refArray: any[]){
  service.search(term)
    .subscribe(
      result => {
        refArray = result;
        console.log(refArray); // display result as it should
        console.log(this.resultArray); // display "undefined"
      },
      err => {
        refArray = [];
        console.log(err);
      },
      () => {console.log('completed')}
    );
 }

And then, I use it like this:

this.search(
  searchTerm,
  this.Service,
  this.resultArray,
);

the problem is that my component level declared variable "apiSpots" never get the result from the subscription, even though the "refArray" parameter from the function gets it normally. I suspect the "refArray" from inside the subscription isn't exactly the same as the one I pass to the function, but I don't know how to solve it.

Any Ideas?

EDIT: typo

SOLUTION:

So, thx to martin, here's the modified sample: search function

search(term: string,service,callback: (result) => void){
service.search(term)
  .subscribe(
    result => {
      callback(result);
    },
    err => {
      callback([]);
      console.log(err);
    },
    () => {console.log('completed')}
  );
}

and usage

let context = this;
 this.search(
    searchTerm,
    this.Service,
    function(result){
      context.resultArray = result;
    },
  );

Upvotes: 1

Views: 74

Answers (2)

martin
martin

Reputation: 96891

If I understand your problem correctly you want to assign the result from service.search(term) to the refArray array passed as reference.

If you use just this:

refArray = result

Then you're overriding the local variable refArray with result but this has no effect on the reference (the original array). You just re-assigned this local variable called refArray.

You could, however, use any method that modifies the array in-place. For example call refArray.push() in a loop to append all items from the result array. Nonetheless, this isn't the best way to use Rx.

Instead return the Observable:

search(term: string,service,refrray: any[]){
  return service.search(term);
}

And then subscribe to it when you need it:

this.myService.search(...).subscribe(result => this.resultArray = result);

Alternatively, if you really need to use it the way you're using it right now don't pass the refArray and use callback function instead that assigns the result to this.resultArray in local scope. Then when calling the search() method you'll just call the callback from the subscribe() call.

Upvotes: 3

M. Samardžić
M. Samardžić

Reputation: 51

you have put the result values from the service to the refArray, so it is normal that it shows the values, but the resultArray is undefined, because you did passed it as the undefined. Mistake happened before calling the function.

Upvotes: 0

Related Questions