How to fix error: Syntaxerror: JSON Parse error: Unexpected Identifier "Undefined"

Asked

Viewed 731 times

1

I am getting the following error while trying to test my function:

Syntaxerror: JSON Parse error: Unexpected Identifier "Undefined"

The function is this:

function onGetInstanceName(e, args) {
    rootScope.instance_name = _storage.local.get("instance_name") || _storage[storage].get("instance_name");
    rootScope.instanceSelected = JSON.parse(_storage.local.get("instanceSelected")) || JSON.parse(_storage[storage].get("instanceSelected"));
    // console.log(rootScope.instanceSelected);
    log("info", rootScope.instance_name);
    if (null === rootScope.instance_name || '' === rootScope.instance_name) {
        log("info", $state.$current.name, ' ', $state.$current.url);
    }
    rootScope.instance_id = parseInt(_storage[storage].get('instance_id')) || rootScope.instance_id;
    rootScope.$broadcast('getOperatorLogged', null);
}

And the test is being done as follows:

it('testing if the onDisplaySelectionInstance function', function(){        
    srv.onDisplaySelectionInstance();
    rootScope.$emit('displaySelectionInstance', null);   
});

What I have to do to correct that mistake?

  • Try to give console.log in _storage.local.get("instanceSelected") and _storage[storage].get("instanceSelected"), likely that any of them are undefined.

  • Before JSON.parse, try to check what’s coming, to see if json is complete.

  • @Lucascosta after giving console.log obtained that: _Storage.local.get("instanceSelected") is null and _Storage[Storage]. get("instanceSelected") is Undefined. What I need to do now?

  • 1

    Do not need to duplicate the comments Alexia, we will see. You need to tell us or find out what they are and why they are null.

  • @Lucascosta has some way I can define it?

  • To be honest, what is their origin? In this case what is _storage, etc...

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.