3

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?

5
  • It is difficult to understand what you are trying to do. Where is the matching? Commented Jan 5, 2017 at 8:12
  • I mean when I pass unitId = 1, I should get result from api only matching to unitId = 1. For now, I am getting all the results related to 1, 1.1, 2.1, 3.1 etc.. i.e whichever unitId contains 1 in it. Commented Jan 5, 2017 at 9:19
  • how does your matching algorithm on the api look like? It should be a method similar to equals(unitId) instead of match(unitId), because "2.1", "3.1", and "1" are all a match to "1". However, equals(unitId) will only return results with the unitId you give to the api. Can you provide your api code? Commented Jan 5, 2017 at 9:24
  • I am using in-memory web api provided by Angular2. I haven't wrote any code for api. This API is for testing purposes only. This is the api: github.com/angular/in-memory-web-api Commented Jan 5, 2017 at 9:26
  • @Jayant Did you get any unit tests for the services calling in-memory-web-api? Commented Sep 1, 2017 at 16:48

1 Answer 1

3

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

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

1 Comment

Awesome! This is what exactly I was looking for. 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.