How to match exact url parameters passed in in-memory web api in Angular2?

1.3k views Asked by At

I am using a parameter "UnitID" to get data for this particular unit id using following code:

this.unitDetailsService.getUnitDetailsbyId(this.activeUnitId)

I am using this activeUnitId parameter to construct a url to be used in in-memory service using following code:

const url = `${this.unitDetailsUrl}/?unit_id=${unitId}`;

I am getting desired results for unit id values like 1.1, 2.1, 3.1, 3.2 etc. However, I am getting undesired results for unit id values like 1, 2, 3 etc. For unit id 1, I am getting all the values which are related to 1, 1.1, 2.1, 3.1. Similarly for unit id 2, result consists 2, 2.1, 2.2, 3.2. for unit id 3, it is 3, 3.1, 3.2, 3.3, 3.4.

I think it is matching to all those values which consists passed unit id rather than matching exact url. Can someone help me to do an exact match?

1

There are 1 answers

1
John On BEST ANSWER

I see. I assume that ${unitId} will be replaced by e.g. 1. Try adding ^ and $ at the end like this

${this.unitDetailsUrl}/?unit_id=^${unitId}$

e.g detailedUrl/?unit_id=^1$

The library use the parameters as though it was a regular expression. Adding ^ means to look at the start of the string, and $ means the end of the string. Passing 1, will then only match 1, not 2.1 or 3.1