Unable to check if null - node.js

I don't understand why I am unable to check null? Here is my code,
Tag.find({name : 'foo'}).exec(function(tag){
console.log('tag : ' + tag); // It's a null
console.log('type : ' + typeof tag); // It's a object
if(!tag){console.log('true');}
else {console.log('false');}
});
I try a lot of way such as,
if(tag == null){console.log('true');}
else {console.log('false');}
In every way, I always got else condition though I got null from tag.

In JavaScript, typeof null === 'object' has always been the case. It's just one of those gotchas you have to remember. If you want to check for strictly null, do a strict equals check: tag === null. tag == null will return true if tag is null or if it is undefined.

Related

preventing value compilation errors

Bixby raises "Value Compilation Errors" in the debugger whenever I run a script that brings back Empty Optional Values. The variable model is correct in that these values really are optional, i.e not needed for the user task to succeed. So you can simply ignore the debugger errors and everything will work, EXCEPT that the Story testing tool will report these runs as "failed." This means that the Story tool is almost useless for these cases, and that's a big problem.
Can someone show me how to code to avoid them?
Thanks to a tip from #mincheng I finally figured out how to do this. The key is to delete any object properties that are null, undefined, or empty strings. I loop over my array of objects with the delete function.
// remove undefined properties here
const removeEmpty = (obj) => {
Object.keys(obj).forEach(key => {
if (obj[key] && typeof obj[key] === 'object') removeEmpty(obj[key]);
else if (obj[key] === undefined || obj[key] === null || obj[key] === "") delete obj[key];
});
return obj;
};
for (var i = 0; i < altBrainsData.length; i++) {
//console.log('i is', i);
//console.log('function object', i, 'is', altBrainsData[i])
//console.log(removeEmpty(altBrainsData[i]))
altBrainsData[i] = removeEmpty(altBrainsData[i])
}

using Object name in if statement both true and false?

I try to use my object name for an if statement.. but both come up as true, why?
var moduleInfo = new Object("moduleInfo");
moduleInfo ["name"] = "Module: Export"
if (moduleInfo !== "moduleInfo"){
console.log("window is NOT modulInfo")
}
if (moduleInfo == "moduleInfo"){
console.log("window IS modulInfo")
}
The !== is comparing by type, and you are comparing an object with a primitive type of string. replacing either that operator with != or replacing the second one with === will probably get you a more consistent/desired result.
== converts the operands to the same type before making the comparison
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators

JavaScript false Boolean evaluates as true after assign as false

My variable keeps evaluating as true and goes inside the IF block. How is this possible? Even after I assign it as false.
this.model.hasQ1 = false
console.log('type ', typeof this.model.hasQ1) //don't know why typeof is a string
//const test = this.model.hasQ1 === 'true' //this will error out, but shouldn't if the top is typeof is a string
if(this.model.hasQ1){ //this should be false
console.log('hasQ1 ', this.model.hasQ1) //this gets printed anyways
}
//output: hasQ1 false
Okay, so did the toString() trick and it evaluates as expected. Can someone explain why? FYI, this is from a Typescript class, but I usually don't have to do this.
//model was assign this way, yet appears as a string (see next line)
this.model.hasQ1 = false;
//console log of this model appears like this
....,
hasQ1: 'false' }
//This fixes the issue
if(this.model.hasQ1.toString() === 'true'){
console.log('I should not see this')
}
you can force to compare the variable as string to make sure it works:
if(this.model.hasQ1.toString() == "true"){ //this should be false
console.log('hasQ1 ', this.model.hasQ1) //this gets printed anyways
}
var model = { hasQ1: null };
model.hasQ1 = false;
console.log('type ', typeof model.hasQ1) //typeof is boolean
if (model.hasQ1) { //this is false
console.log('hasQ1 ', model.hasQ1) //this is not printed
}
//output: hasQ1 false
You have to provide the class in order to figure out what's wrong with your code.

Node-red not detecting null value

I am trying to build a flow, that reads sensor data and transfers by CANBUS. Sometimes, null data is coming what I mean is tempInt or ldrInt could be null.
var msg2 = {
payload:
{
"$class": "org.acme.testnetwork.UpdateSensorData",
"sampDevice": "houseMehmet",
"newTempVal": tempInt,
"newLightVal": ldrInt,
"timeStamp": Date().toString()
}
};
Although I can access msg.payload.newLightVal and set it to any value, in the case that its value is null, the control statement such below fails.
if(msg.payload.newLightVal===null){
msg.payload.newLightVal = -1 ;
}
Are you sure that tempInt and ldrInt are null and not undefined .
I think they may be undefined in which case replace === with == in the if statement i.e
if(msg.payload.newLightVal==null)
Edit : Since the msg.payload.newLightVal is NaN , this should be the if clause
if(msg.payload.newLightVal==null || isNaN(msg.payload.newLightVal))

Value rule for Lookup field in RibbonWorkbench

I want to add a display rule to a custom ribbon button based on the value of a lookup field whether the lookup field is null or not. How should i check it. The below code is not working. I assigned value as 0 , "" but still it was not working. Please give some solution.
Thanks.
<DisplayRule Id="mcg.mcg_transition.DisplayRule_lookup.DisplayRule">
<ValueRule Field="ifx_lookupid" Value="null" Default="true" InvertResult="false" />
</DisplayRule>
Use Enable Rule as it gives the facility to write some JavaScript function.
//Check if the field value is null or not? '
//in my case the field I am checking for null value is ifx_lookupid.
function DisplayRule_IfField_IsNull()
{
var regardingField = Xrm.Page.getAttribute("ifx_lookupid");
if (regardingField != undefined && regardingField != null) {
return true;
}
else {
return false;
}
}
}
Hope this will help.

Resources