How do you check that data.results in the following is not empty before trying to perform actions on it?
$.getJSON(myurl, function(data) {
// if data.results is not empty
// {
// console.log(data.results.size);
// }
});
if (data != null && data.results != null && data.results.length > 0) {
// the array is not empty
}
I usually use something like this:
if (data != null && data.results != null && data.results.size != 0) ...
how about doing this one?
if(data!=undefined){
// do logic here
}
Related
I created this function which should check for every case, and if one of the case not corresponding then i should get immediately the result.
const addText = (data, addAlternative) => {
return (data !== 'N/T' || data === 0 || data) ? data : addAlternative;
};
console.log(addText('N/T', 'alternative'))
in my case addText('N/T', 'alternative') i get N/T, but i expect to get alternative word as a result. How to change my statement to check every situation and if one of the situation occurs i have to get the right answer like in the example that i provided?
You are receiving NT as output, because you are checking the truthy status of data parameter isiide the function by using data !== 'N/T' || data === 0 || data.
Update that to data !== 'N/T' || data === 0, it will work as expected.
const addDefaultTextIfIsEmpty = (data, addAlternative) => {
return (data !== 'N/T' || data === 0) ? data : addAlternative;
};
console.log(addDefaultTextIfIsEmpty('N/T', 'alternative'))
If you want to output data if the param is not defined, just check !data inside the condition, there you dont need to add data === 0 because !data will handle null, undefined, empty, and zero checks
const addDefaultTextIfIsEmpty = (data, addAlternative) => {
return (data !== 'N/T' || !data) ? data : addAlternative;
};
console.log(addDefaultTextIfIsEmpty('N/T', 'alternative'))
You need to prevent if (data) from being tested if data is 'N/T' but currently it is being evaluated at the end because of short-circuiting.
Your logic is basically:
return (false || false || true) ? ...
^ ^ \____ because data contains 'N/T'
| |
| '-------- data is not 0
because data is N/T
This obviously resolves to true because the || operator only needs one of the expressions to be true.
What you actually want is:
return (data !== 'N/T' && (data === 0 || data)) ? ...
^
|
remember De-Morgan's Theorem
(google it if you don't know)
const addText = (data, addAlternative) => {
return (data !== 'N/T' || data === 0) ? data : addAlternative;
};
const addText = (data, addAlternative) => {
return ((data && data !== 'N/T') || data === 0) ? data : addAlternative;
};
console.log(addText('N/T', 'alternative'))
/* Try below */
const addDefaultTextIfIsEmpty = (data, addAlternative) => {
return (data !== 'N/T' || data === 0) ? data : addAlternative;
};
addDefaultTextIfIsEmpty('N/T', 'alternative')
You are using data in if statement and you are passing 'N/T' which changes the value to false and according to the logic the value always be true. Try this solution.
const addText = (data, addAlternative) => (data && (data !== 'N/T' || data === null || data === 0)) ? data : addAlternative;
console.log(addText('N/T', 'alternative'))
I need to check if there is any key in localStorage
This code did not work
if (localStorage.user === undefined) {
alert('There is no key!');
}
if (Object.keys(localStorage).length === 0) {
alert('There is no key!');
}
You need to use getItem
if (localStorage.getItem("user") === null) {
alert('Não há chave!');
}
EDIT
You can check if the localstorage is empty by
if (localStorage.length == 0)
I have function below. It does what it needs to except that it does not return the error string I want.
It always returns "".
I've put breakpoints and seen it step into each error case, but it doesn't return there. It returns at the end of the function.
I'm lost, I'm sure I'm making a really stupid mistake but I don't get it...
Save me the few hairs I have please :)
public validatePanel = () => {
this.Queries().forEach(function(q, i) {
if(q.from() == "" || q.from() == null || q.from() == undefined) {
return "Please select a database";
}
if(q.select().length > 0) {
q.select().forEach(function(s, j) {
if(s.selectoption() == "" || s.selectoption() == null || s.selectoption() == undefined){
return "Please select a stat to show";
}
});
}
if(q.where().length > 0) {
q.where().forEach(function(w, j) {
if(w.whereoption() == "" || w.whereoption() == null || w.whereoption() == undefined){
return "Please select a filter to filter on";
}
if(w.wherevalue() == "" || w.wherevalue() == null || w.wherevalue() == undefined) {
return "Please select a value for your filter";
}
});
}
});
return "";
}
As pointed out by Alex Bykov, your forEach function is not causing a return.
Your question on why not, per the MDN
The return value of the function is undefined
Return
value undefined.
Which means nothing you can do will generate a return value you can use. Also per the MDN there is no way to stop or break the loop other than throwing an exception.
There is no way to stop or break a forEach() loop other than by
throwing an exception. If you need such behavior, the forEach() method
is the wrong tool, use a plain loop instead. If you are testing the
array elements for a predicate and need a Boolean return value, you
can use every() or some() instead. If available, the new methods
find() or findIndex() can be used for early termination upon true
predicates as well.
Which means you will need to throw your exception in the forEach loop and then catch the exception and return the string like below
(unless you use a normal for loop then you can do whatever you please)
try {
this.Queries().forEach(function(q, i) {
if(q.from() == "" || q.from() == null || q.from() == undefined) {
throw "Please select a database";
}
if(q.select().length > 0) {
q.select().forEach(function(s, j) {
if(s.selectoption() == "" || s.selectoption() == null || s.selectoption() == undefined){
throw "Please select a stat to show";
}
});
}
if(q.where().length > 0) {
q.where().forEach(function(w, j) {
if(w.whereoption() == "" || w.whereoption() == null || w.whereoption() == undefined){
throw "Please select a filter to filter on";
}
if(w.wherevalue() == "" || w.wherevalue() == null || w.wherevalue() == undefined) {
throw "Please select a value for your filter";
}
});
}
});
}
catch(err) {
console.log(error);
}
I have the following code, and i'm trying to protect when an error happens / no data is returned. As it stands, I get the error:
[Info] undefined (ionic.bundle.js, line 19387)
[Error] Error: undefined is not an object (evaluating 'data.data.length')
The code is like the following, understandably it's falling over because data.data.length is null / undefined (no data returned on purpose to test)
SaveSubmitService.saveLocal('GetData', 'NearByHubs4S', $scope.options, false).then(function (data) {
$scope.return = data;
if (typeof data != "undefined") {
if (data.data.length > 0) {
$scope.bars = $scope.return.data;
$scope.noNot = false;
} else {
$scope.noNot = true;
}
} else {
$cordovaDialogs.alert('Could not retrieve data. Are you sure you\'re online?', 'No Response', 'Ok');
}
$scope.$broadcast('scroll.refreshComplete');
});
Oddly, it's going through the typeof as true and falling over at data.data.length being null.
I've tried doing if data == null, data == undefined, data == "undefined", data.data.length == undefined etc.
Basically i'm trying to error if the length is null!
The problem is not that data.data.length is null. The problem is that you want to use length property on undefined. That means that data is defined and non-null, but it does not have a data property, so data.data is undefined.
Instead of:
if (typeof data != "undefined") {
if (data.data.length > 0) {
Do:
if (data && data.data) {
if (data.data.length > 0) {
It looks to me like your guard isn't sufficient, you need to check data and also data.data:
$scope.return = data;
if (data && data.data) {
if (data.data.length > 0) {
$scope.bars = $scope.return.data;
$scope.noNot = false;
} else {
$scope.noNot = true;
}
} else {
$cordovaDialogs.alert('Could not retrieve data. Are you sure you\'re online?', 'No Response', 'Ok');
}
The guard data && data.data will only be true if data is truthy (any value other than null, undefined, 0, "", NaN, or false, which are all falsy) and also if data.data is truthy. It won't try to check data.data if data is falsy.
You seem to be only checking the data variable, and not its data member.
Try using an if statement such as if (data && data.data & data.data.length > 0) to check for null data.
Try this
if (data && data.data) {
if (data.data.length > 0) {
...
} else {
....
}
}
In the following IF statement, one of the conditions is sometimes null.
Parse.User.current() can be null, in which case I'll get this error:
Uncaught TypeError: Cannot read property 'id' of null
Is there an elegant way to avoid this error?
if( post.get("parent").id != Parse.User.current().id ) {
}
A cleaner way can be :
var current = Parse.User.current();
if(current && post.get("parent").id !== current.id ) {
}
Then check like this
if(Parse.User.current() !== null)
{
if( post.get("parent").id !== Parse.User.current().id ) {
//Do whatever necessary
}
}