Search code examples
javascriptjsonangularjsrestangular

Null check on returned JSON value in Angular/Restangular not working


I have the following string being returned from a rest service call:

            {"fooName":""}

In an angular controller, I have:

var aFoo= Restangular.one(theRoute).get()

I thought this would resolve to true, but it is false:

aFoo.fooName.length === 0

When I log the following, I get 0:

aFoo.fooName.length

My ultimate goal is to just check if there is a fooName or not, but I cannot figure it out.


Solution

  • I think you want to use typeof: if it is undefined, you have an empty JSON Object. Like so:

    if (typeof(aFoo.fooName) != "undefined"){
        //something is here
    

    More information here