Search code examples
javascriptthunderbird-addon

can't convert undefined into an object


I am getting the error "Can't convert undefined to object on the line return hasOwnProperty(prop); and I simply cannot figure out where the problem lies. I can post more of the code if necessary.

getCardProperty : function (card, prop, def) {
    if (typeof def === "undefined") {
        def = null;
    }

    // json synckolab object
    if (card.synckolab) {
        if (card.hasOwnProperty(prop)) // TODO better check for undefined?
        {
            return hasOwnProperty(prop);
        }
        return null;
    }

Solution

  • hasOwnProperty(prop) doesn't exist - you need to qualify it with an object name. Just change it to card.hasOwnProperty(prop).

    You can simplify it further:

    if (card.synckolab) {
        return card.hasOwnProperty(prop) || null;
    }
    

    This will return true or null. Or you can simplify even further:

    if (card.synckolab) {
        return card.hasOwnProperty(prop);
    }
    

    This will return true or false.