object.valueOf() if returns false [duplicate] - javascript

This question already has an answer here:
Why do my MongooseJS ObjectIds fail the equality test?
(1 answer)
Closed 7 years ago.
I am baffled as to how if classes in JavaScript work, I've always known it's slightly more complicated but now I just have no idea. Code and output below.
The following json's are stored in mongodb. I'm suspecting this might be where the error is..
console.log(JSON.stringify(user)):
{"_id":"5687f787f8ad41175fab5bd5","pic":"karl.png","language":"5687f787f8ad41175fab5bd2","cell":1,"local":{"email":"karl.morrison#email.com","password":"12345"},"profile":{"name":"Karl Morrison"},"sessions":[{"id":"5687f79bf8ad41175fab5bd9","seen":false,"active":false}]}
console.log(JSON.stringify(message)):
{"authorId":"5687f787f8ad41175fab5bd5","upvotes":0,"created":"2016-01-02T16:15:23.621Z","message":"<p>aa</p>"}
Ze code:
console.log('"' + user._id.valueOf() + '" "' + message.authorId.valueOf() + '"');
console.log({}.toString.call(user._id).split(' ')[1].slice(0, -1).toLowerCase());
console.log({}.toString.call(message.authorId).split(' ')[1].slice(0, -1).toLowerCase());
if (user._id.valueOf() == message.authorId.valueOf()) {
console.log('TRUE');
} else {
console.log('FALSE');
}
Console:
"5687f787f8ad41175fab5bd5" "5687f787f8ad41175fab5bd5"
object
object
FALSE
I don't understand why TRUE isn't returned?

According to mongodb documentation:
Changed in version 2.2: In previous versions ObjectId.valueOf()
returns the ObjectId() object.
Since user._id and message.authorId are ObjectId objects the regular == compares their references thus you get false.
Try using either:
user._id.toString() == message.authorId.toString()
user._id.equals(message.authorId)
upgrading to newer mongodb driver.

Related

Filter String/Array for multiple conditions [duplicate]

This question already has answers here:
multiple conditions for JavaScript .includes() method
(19 answers)
Closed last year.
This is my code below, basically i want the bot to check two things. The users message needs to contain "how" + either "doing" or "bread". It works perfectly when i use only "doing" but not when I add the "bread" condition.
I need a clean and simple solution for this and hope someone can help me, bc for me thats the most logical way in archiving what i need :D
if(msg.content.includes("how") && msg.content.includes("doing" || "bread") ){
if(msg.author != token){
msg.lineReply("I am good sir")
}
}
if(msg.content.includes("how") && (msg.content.includes("doing") || msg.content.includes("bread")) ){
if(msg.author != token){
msg.lineReply("I am good sir")
}
}

Ignore javascript errors like in php [duplicate]

This question already has answers here:
Test for existence of nested JavaScript object key
(64 answers)
Closed 2 years ago.
I'm parsing some JSON with jQuery into array.
So there is a place where i'm assigning this:
$("#address_text").text(data['account']['address']['text']);
The problem is that sometimes i don't have this in the JSON and i've got error:
TypeError: null is not an object (evaluating 'data['account']['address']')
And the script is blocked under that line.
Is there anyway that i can ignore the error and assign nothing to #address_text ?
I search something like "#" sign in php. That no matter what is the error, just ignoring it.
First: If you have an error, fix the error not ignore it.
Second: Check if value exist before get the property
if (data && data['account'] && data['account']['address'] && data['account']['address']['text']) {
....
}
How about this?
if(data && data.account && data.account.address && data.account.address.text){
$("#address_text").text(data['account']['address']['text']);
}else{
$("#address_text").text("data is not valid");
}

Cloud function query returning null even though it should return a value [duplicate]

This question already has answers here:
Firebase Query Double Nested
(3 answers)
Closed 5 years ago.
This is the structure of the database,
Content {
"randomID" {
"randomID2" {
"text": "Hello World"
}
}
}
I'm trying to do a query in cloud functions, to check if text is equalTo a specific value but it always returns null
admin.database().ref('Content').orderByChild('text').equalTo("someText").once('value').then(snapshot => {
console.log(snapshot.val()); -> this returns null
})
I'm pretty sure the issue is that I have two random ID's there, also wildcards don't work for .once('value') like they do for onUpdate I believe. Or I'm doing something wrong as I'm quite new to Javascript.
admin.database().ref('Content').orderByChild('text')
should be changed to
admin.database().ref('Content/{randomID}/{randomID2}').orderByChild('text')
you can't skip the ids in the reference

How to put a or condition to .where function in underscore.js [duplicate]

This question already has an answer here:
How to use _.where method from underscore.js library for more elaborated searchs
(1 answer)
Closed 7 years ago.
I want to check or condition if the author is shakespeare it should return the value or if the year is 1611 also it should return the value.. how is the syntax in underscore
var a=_.where(listOfPlays, {author: "Shakespeare", year: 1611});
where uses AND logic.
For a different kind of condition consider using filter, giving it a relevant predicate, eg.
var result = _.filter(listOfPlays, function(play){
return play.author=="Shakespeare" || play.year==1611;
});

Array.indexOf Not working [duplicate]

This question already has answers here:
Why Array.indexOf doesn't find identical looking objects
(8 answers)
Closed 7 years ago.
I have an angularJS/Typescript application where I am trying to check if an object is already in a current list of objects
if (this.selectedFormatData.indexOf(item) === -1) {
//doesn't exist so add
this.selectedFormatData.push(item);
} else {
this.selectedFormatData.splice(this.selectedFormatData.indexOf(item), 1);
}
I have used this code before and it worked but isn't in this instance. Console output suggests it should work?
Any ideas?
Update: yeah correct looks like a duplicate sorry. I had a previous bit of code where i thought it worked because it was returning 0 instead of -1. Not sure why it would return 0 though
As the comments state, indexOf() is not meant to compare objects. This has been answered before here: Why Array.indexOf doesn't find identical looking objects.

Categories

Resources