Comparison of two uuids in Node.js

I have a question that I could not find in my research on the Internet. I am working on a web application in Node.js and Cassandra. I am currently working on a notification system, and I need to compare two uuids so that I do not send a notification to those people who take the original action (which causes the notification).

The problem is that when comparing two uuids that should be equal, I always get a false value.

Here is an example of the code I'm currently working on:

console.log('user_id :', user_id.user_id); console.log("user id of the current user :", this.user_id); console.log(user_id.user_id == this.user_id); console.log(user_id.user_id === this.user_id); 

And here is the result display:

 user_id : Uuid: 29f1227d-58dd-4ddb-b0fa-19b7fc02fbe8 user id of the current user : Uuid: 29f1227d-58dd-4ddb-b0fa-19b7fc02fbe8 false false user_id : Uuid: c8f9c196-2d63-4cf0-b388-f11bfb1a476b user id of the current user : Uuid: 29f1227d-58dd-4ddb-b0fa-19b7fc02fbe8 false false 

As you can see, the first uuids should be the same. They are generated using the uuid library inside the nodejs cassandra driver. I do not understand why I can not compare them when I can make any query in my Cassandra database with the specified uuid.

If someone can help me, it will be very nice!

+6
source share
4 answers

As Ary noted, the contents are the same, but there are no addresses, so the comparison returns false.

The cassandra driver's UUID object provides an equal function that compares the original hexadecimal string of the UUID contents that you could use to do this:

 > var uuid1 = uuid.fromString('29f1227d-58dd-4ddb-b0fa-19b7fc02fbe8') > var uuid2 = uuid.fromString('29f1227d-58dd-4ddb-b0fa-19b7fc02fbe8') > uuid1 == uuid2 false > uuid1 === uuid2 false > uuid1.equals(uuid2) true 
+4
source

The content is the same, but there should be no addresses. If your comparaison returns false, it could be your variables of type Object.

+1
source

I am doing something like this and it works:

 // assuming there are 2 uuids, uuidOne uuidTwo uuidOne.toString() === uuidTwo.toString() 
0
source

It looks like your user_id is actually an object containing "Uuid" and not Uuid itself. The user_id objects do not match, but they contain the same data.

Try comparing Uuid directly like this:

 console.log(user_id.user_id.Uuid == this.user_id.Uuid); console.log(user_id.user_id.Uuid === this.user_id.Uuid); 
-1
source

Source: https://habr.com/ru/post/985691/


All Articles