HOW TO SAVE A SUBSCRIBE RETURN IN A VARIABLE?

Asked

Viewed 109 times

0

My basic code:

ngOnInit() {
const today = Date.now()
this._schedulesService.getSchedules().subscribe(      
  (res: SchedulesModel[]) => {
    this.schedulesToday = res
    this.schedulesToday.filter((schedule: SchedulesModel) => schedule.dtPlanejada == today.toString())
    this.schedulesToday.forEach((obj) => {
      obj.dtPlanejada = formatDate(obj.dtPlanejada)
      obj.containerdata = this._containersService.get__container__by_id(obj.idContainer)
    })
  }
)

}

Snippet of code connecting to my server.

get__container__by_id(id: string): Observable<ContainersModel> {
const token: any = JSON.parse(localStorage.getItem('user')).token    
return this.http.get<ContainersModel>(`${API_URL}/containers/${id}`, setTokenHeader(token)).pipe(map(response => response))

}

My question is: How should I do for that function get__container_by_id(id) return my direct object so that the return is stored directly in the variable obj.containerdata ?

  • I don’t quite understand your context, your problem is that the return of the request is not being assigned to ob.containerdata? Because it’s an http request, I think you should subscribe to get_container_by_id as well, and when you get the response from the server assign the containerdata, if you can explain the context a little better maybe I can help better

  • Exactly that’s the problem. I don’t know how to write the correct mapping of the server response. I have already subscribed to two methods and yet the answer remains a Subscriber object. How should I write to that method get__container_by_id() returns a mapped object in type ContainersModel ?

1 answer

0

Dude, overall, your code is really bad

ngOnInit() {
const today = Date.now()
this._schedulesService.getSchedules()
.pipe(
     map(response=>response.filter(item=>item.dtPlanejada == today.toString()).map(obj=>obj.id)),
mergeMap(
    ids=> {
      return combineLatest(ids.map(id=> this._containersService.get__container__by_id(id))).pipe(map(objs=> {
        return {
          objs
        }
      }))

 ).subscribe(response=>console.log(response)
  • Yours did not improve anything and it didn’t work. But I solved, thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.