0

I get TypeError: Cannot read property 'enabled' of undefined both in the anonymous function (on column 21) and the while loop (on column 15):

var enabled = "unknown";
chrome.runtime.sendMessage({request: "Am I enabled?"}, 
    function(response)
    {
        enabled = response.enabled;
    });

while(enabled == "unknown")
{
    // wait
}

I don't typically write Javascript, so I'm not sure what I could be doing wrong here. Searching gives me results like var y = null; log(y.property); which are not this issue at all.

demize
  • 364
  • 1
  • 15
  • 3
    The error message comes from the anonymous function, and tells you, that `response` is not defined. Check what you're actually passing to the function when you're calling it. – Teemu Nov 18 '15 at 18:36
  • I dont know the `sendMessage` function, but the first parameter of async methods is often the error, which will be `undefind` on success. – t.niese Nov 18 '15 at 18:37
  • @Teemu It comes from both the anonymous function *and* the while loop. I'll edit the post to clarify that. – demize Nov 18 '15 at 18:39
  • I guess `chrome.runtime.onMessage.addListener` function is returning the response in some asynchronous callback like `$.get` or XMLHttpRequest, in which case the onMessage callback should first `return true`: [Chrome Extension Message passing: response not sent](http://stackoverflow.com/a/20077854) – wOxxOm Nov 18 '15 at 18:41
  • 2
    Apart from anything said here before, you can't wait in a `while` loop, it will block all the other execution. Please read [How to return the response from an asynchronous call](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call/14220323#14220323). – Teemu Nov 18 '15 at 18:43
  • 1
    @Teemu That's a good point, and I'll move the rest of the extension logic into the callback now. There's no valid reason to have it outside the callback. – demize Nov 18 '15 at 18:50

1 Answers1

3

The error comes from this line:

 enabled = response.enabled;

because response is undefined.

According to the documentation:

If an error occurs while connecting to the extension, the callback will be called with no arguments and runtime.lastError will be set to the error message.

So, modify your code:

var enabled = "unknown";
chrome.runtime.sendMessage({request: "Am I enabled?"}, 
function(response)
{
    if (!response) {
        // TODO: Check runtime.lastError and take an appropriate action
    } else {
        enabled = response.enabled;
        if (enabled) {
           // TODO: Do the stuff you were planning to do after the while() loop, call a function, etc.
        }
    } 
});
Jared Dykstra
  • 3,596
  • 1
  • 13
  • 25
  • I was caught up on the error reading it the second time, but that was in fact the source of the error. Turns out I forgot to actually add the listener, and it works fine now. – demize Nov 18 '15 at 18:48
  • Bonus: Use promises instead of the callbacks.... It might take you a couple minutes to read up on it, but they make the code so much more maintainable. – Jared Dykstra Nov 18 '15 at 18:50