FindIndex value continuously returning as a -1?

Viewed 51

I have the following expressions:

This fetches the ID parameter passed through the URL:

editUserId = this.route.snapshot.paramMap.get('id');

Then i used FindIndex on an array of objects to find the index of an element in the array equal to the above mentioned ID value fetched from the URL:

this.userToUpdate = this.allUsers.findIndex((x: any) => x.UserId === this.editUserId);

(this.allUsers) being my array of objects.

Although the findIndex continuously returns the value -1 .. I have tried the following two methods, both of which return the same error: "This condition will always return 'false' since the types 'number' and 'string | null' have no overlap":

this.userToUpdate = this.allUsers.findIndex((x: any) => parseInt(x.UserId) === this.editUserId);

this.userToUpdate = this.allUsers.findIndex((x: any) => Number(x.UserId) === this.editUserId);

Any reason as to why this may be happening?

1 Answers

Seventh, I guess that can be the editUserId needs to be number, you can try it:

this.userToUpdate = this.allUsers.findIndex((x: any) => Number(x.UserId) === Number(this.editUserId));

or

editUserId = this.route.snapshot.paramMap.get('id');
editUserId = Number(editUserId)

I guess it because when you return the params to your variable, always it will be string

Related