Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing two uuids in Node.js

I have a question I was not able to find any answer with my research on the web. I work on a web application in Node.js and Cassandra. I'm currently working on a notification system and I have to compare two uuids to make sure I don't send a notification to the people who make the original action ( which provoke the notification ).

The problem is that, when I compare two uuids which are supposed to be equals, I always get a false value.

Here is the example of 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 display of the result :

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 are supposed to be the same. They are generated with the uuid library inside the nodejs cassandra driver. I don't understand why I can not compare them when I am able to make any request on my Cassandra database with a uuid specified.

If someone could help me, it will be a great pleasure !

like image 994
Orodan Avatar asked Apr 22 '15 15:04

Orodan


2 Answers

As Ary mentioned, the contents are the same but the addresses are not, so the comparison returns false.

The cassandra-driver's UUID object provides an equals function which compares the raw hex string of the contents of the UUID that you could use for 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
like image 84
Andy Tolbert Avatar answered Sep 28 '22 02:09

Andy Tolbert


The content is the same but their addresses should not be. If your comparaison returns false it may be your variables are Object type.

like image 22
Ary Avatar answered Sep 28 '22 01:09

Ary