Your code will retrieve the value of the length property and attempt to convert it to a Boolean for the purposes of the if/then test, but this will throw an error if the value happens to be null.
Also, if your test is simply: app.subject.name, you will get a false positive if the value happens to be a falsy value, like 0 or false, which are both perfectly valid values.
With strings, the simplest test is to check for a non-empty string and non-null. If the value was provided by an end-user, it's a good idea to call .trim() on the string first to remove any leading or trailing spaces that may have been inadvertently added.
var myObj = {
test : 0,
testing : null
}
// This will fail with an error when the value is null
/*
if(myObj.testing.length){
console.log("The testing property has a value.");
} else {
console.log("The testing property doesn't have a value.");
}
*/
// This will return a false positive when the value is falsy
if(myObj.test){
console.log("The test property has a value.");
} else {
console.log("The test property doesn't have a value."); // <-- Incorretly reports this
}
// This explicit test will pass and fail correctly
if(myObj.testing !== "" && myObj.testing !== null){
console.log("The testing property has a value.");
} else {
console.log("The testing property doesn't have a value.");
}
Also, if the value is there, put your code in the ifs true branch and don't worry about return false.
categories.fetch = function(app){
if(app.subject.name !== "" && app.subject.name !== null) {
var p =
Restangular.all('v1/categories').getList({app.subject.name.toUpperCase()});
}