0

I am trying to make call from my angular service to loopback api. I have a parcelStatuses collection that contains a parcelId so i am able to include parcel collection too but I also need to check against a particular vendorId and that vendorId exists in parcel collection. I am trying to make use of scope to check against particular vendorId but i think i am not writing correct json syntax/call. Here is my function inside service

private getParcelsByFilter(
  limit: number,
  skip: number,
  vendorId: string,
  filter: string
) {
  const checkFilter = {
  "where": {
    "and": [{"statusRepositoryId": filter}]
  },
  "include": [
      {
        "parcel": [
          {
            "scope": {"vendorId": vendorId}
          },
          "parcelStatuses", 
          {"customerData":"customer"}
        ]
      }
    ],
    "limit": limit,
    "skip": skip,
  }

  return this._http.get<IParcel[]>(
    `${environment.url}/ParcelStatuses?filter=${encodeURIComponent(JSON.stringify(checkFilter))}`
  );
}

Here is my demo view of parcelStatus collection object

[{
 "id":"lbh24214",
 "statusRepositoryId":"3214fsad",
 "parcelId":"LH21421"
}]

Demo json of parcel

[{
 "id":"LHE21421",
 "customerDataId":"214fdsas",
 "customerId":"412dsf",
 "vendorId":"123421"
}]

Please help me with writing correct call

0

1 Answer 1

1

Formatting aside, there's several issues with the query:

Unnecessary and

This line:

where: {
  and: [{statusRepositoryId: filter}]
}

Can be simplified to:

where: {
  statusRepositoryId: filter
}

As there is only 1 where condition, and becomes redundant.

Misuse of include and scope

include is used to include relations while scope applies filters to those relations. They can work in tandem to create a comprehensive query:

include: [
  {
    relation: "parcels",
    scope: {
      where: {vendorId: vendorId},
    }
  }
],

This will include the parcels relation as part of the response, while filtering the parcels relation with a where filter.


That means the final code should look similar to the following:

private getParcelsByFilter(
  limit: number,
  skip: number,
  vendorId: string,
  filter: string
) {
  const checkFilter = {
  where: {statusRepositoryId: filter},
  include: [
      {
        relation: "parcels",
        scope: {
          where: {vendorId: vendorId},
        }
      }
    ],
    limit: limit,
    skip: skip,
  }

  return this._http.get<IParcel[]>(
    `${environment.url}/ParcelStatuses?filter=${encodeURIComponent(JSON.stringify(checkFilter))}`
  );
}

Further reading

Please review these resources to get a better understanding on how to use filters.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answere Rifa. I got it fixed from backend side by running script on data and added the required property inside the parcelStatus collection. So then and was necessary "where":{"and":[{statusRepositoryId:"3214fsad"},{"vendorId":"123421"}]..}. Also i needed to inlude parcelStatus complete array inside each parcel array again because of some of those values will be used in table. However your solution is right too, thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.