#Data not added to queryParams

10 messages · Page 1 of 1 (latest)

jolly marsh
#

Hello! I'm confused how to add filters to queryParams. Offset and Limit are added but data from "Filter" is not. What am I doing wrong here?

      private reports$ = combineLatest([this.reportListService.query$]).pipe(
    switchMap(([[filter, sort, limit, page]]: [[ReportFilters, any, any, any]]) => {
      const queryParams: any = {};

      this.mapDataToQueryParams(filter, queryParams, limit, page);
      this.sorting(sort, queryParams);
      console.log('filter', filter);
        // filter { createdTimestampFrom: "2021-12-31T22:00:00.000Z"
        // createdTimestampTo: "2022-12-30T22:00:00.000Z"
        // dateType: "CREATED" }
      console.log('params', queryParams);
        // params { offset: 0, limit: 20}

      return fromPromise(this.orderReportRes.query(queryParams));
    }),
    share()
  )

    public mapDataToQueryParams(filter: any, queryParams: any, limit: any, page: any) {
    if (page) {
      queryParams.offset = page.offset;
      queryParams.limit = page.limit;
    }
    if (limit) {
      queryParams.limit = limit;
    }
    if (filter) {
    queryParams.dateType = filters.dateType;
    queryParams.createdTimestampFrom = filters.createdTimestampFrom;
    queryParams.createdTimestampTo = filters.createdTimestampTo;
    }
  }
    ```
jolly marsh
#

If I add this to the reports$ method it works. But why doesn't it work inside the "this.mapDataToQueryParams()"

      if (filter.dateType != null && filter.createdTimestampFrom != null && filter.createdTimestampTo != null) {
        console.log('inhere')
        queryParams.dateType = filter.dateType;
        queryParams.createdTimestampFrom = filter.createdTimestampFrom;
        queryParams.createdTimestampTo = filter.createdTimestampTo;
      }
left sparrow
#

what do you do in this.sorting()?

quiet echo
#

Try not to modify an object by its reference. It's better to change the mapDataToQueryParams in order to return the queryParams

jolly marsh
#

In this.sorting() I do sorting.
So I should return queryParams from the mapDataToQueryParams ?

quiet echo
#

Try also to type your objects
Example:

public mapDataToQueryParams(page: PageType, limit: LimitType, filter: FilterType): QueryParamType {
    return {
      ...(page ? page : null),
      ...(limit ? limit : null),
      ...(filter ? filter : null),
    };
  }

(code not tested)

jolly marsh
#

Okay, thanks. I will try and modify the code

left sparrow
jolly marsh
#

Sorting on first reload is not used. It starts with NULL

left sparrow
#

so have you commented it out and checked the logs?