比较Node.js中的两个uuids
我有一个问题,我无法在网上find任何答案。 我在Node.js和Cassandra的Web应用程序上工作。 我目前正在制定一个通知系统,我必须比较两个uuids,以确保我不会将通知发送给做出原始操作(引发通知)的人员。
问题是,当我比较两个应该是平等的uuids,我总是得到一个错误的价值。
这里是我目前正在使用的代码示例:
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);
这里是结果的显示:
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
正如你所看到的,第一个uuids应该是一样的。 它们是使用nodejs cassandra驱动程序中的uuid库生成的。 我不明白为什么我不能比较他们,当我能够在我的Cassandra数据库与uuid指定的任何请求。
如果有人能帮助我,这将是一个非常高兴!
正如Ary提到的,内容是相同的,但地址不是,所以比较返回错误。
cassandra-driver的UUID对象提供了一个equals函数,该函数比较可用于此的UUID内容的原始hexstring:
> 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
内容是相同的,但他们的地址不应该是。 如果您的比较返回false它可能是您的variables是对象types。
我做这样的事情,它的工作原理:
// assuming there are 2 uuids, uuidOne uuidTwo uuidOne.toString() === uuidTwo.toString()
它看起来像你的user_id实际上是一个对象“包含”一个Uuid,而不是Uuid本身。 user_id对象不相同,但它们包含相同的数据。
尝试直接比较Uuid的:
console.log(user_id.user_id.Uuid == this.user_id.Uuid); console.log(user_id.user_id.Uuid === this.user_id.Uuid);