i am using angular15 and here i need to generate unique id, so the process works perfectly fine, when i add set of code in that respective service file, but as that set of code is used across all service branches, i created a method under shared service and trying to get that data but i get as undefined.
shared.service.ts:
public generateUniqueId() {
return uuid();
}
generateCorrelationId(routerEvent:any,route:string):any {
let previousUrl:any =[];
let uniqueCorrelationId = '';
routerEvent.events
.pipe(filter(event => event instanceof NavigationEnd))
.subscribe(({urlAfterRedirects}: NavigationEnd) => {
previousUrl.splice(0,previousUrl.length-1)
previousUrl = [...previousUrl, urlAfterRedirects];
if(previousUrl.length === 1 && previousUrl[0]=== route) {
return uniqueCorrelationId = this.generateUniqueId();
} else if(previousUrl.length == 2 && previousUrl[1] === previousUrl[0]) {
return uniqueCorrelationId
} else if(previousUrl.length == 2 && previousUrl[1] != previousUrl[0]) {
return uniqueCorrelationId = this.generateUniqueId();
} else {
return uniqueCorrelationId;
}
});
}
component.service.ts:
constructor(private geneateUUid:GenerateUniqueIdService) {
let uniqueIdGeneration = this.geneateUUid.generateCorrelationId(this.router,'/findprofile');
console.log(uniqueIdGeneration,"uniqueIdGeneration")
}
Observation:
in Console, first component console value comes and next respective console under shared service appears.
also here based on particular component service hit, i am checking for generation of new key or retaining it, is it possible to do it dynamically i mean as a common checking whether previous and current routing is matching.
3
Answers
As stated you aren’t returning anything. Personally, I would use
await
to make the code more readable.Try this:
In the converted code, the generateCorrelationId function returns a Promise that resolves to the unique correlation ID. The function waits for the first NavigationEnd event using the first() operator and then uses the resolved event to get the urlAfterRedirects property.
The splice and push methods for the previousUrl array are replaced with an assignment using the spread operator. Finally, the function returns the unique correlation ID that was generated based on the current and previous URLs.
One way to go about this is to declare a
Subject
in shared.service.tsand subscribe to in the component.service.ts
Once the logic in the
routerEvent
subscription is executed and theuniqueCorrelationId
is finalised, thensubject.next(uniqueCorrelationId)
can be called and this will be read in the subscription callback block in the component.service.ts.Refer Code below:
shared.service.ts:
component.service.ts:
Hope this helps.
First of all why
any
?The
generateCorrelationId
function is expected to return a value right? ‘string’ right?, but the subscribe method does not return anything. Therefore, the function will always return undefined.I used to commit this mistake a lot, now I generally promisify my function.
PS. I am not angular expert, I looked at this from a JS POV and that is all. so someone else from angular POV might have a more streamlined answer.