joelsa
joelsa

Reputation: 65

Javascript - cannot read property of undefined

Could someone explain to me why I get the "Property of value is undefined error" please? My Javascript Skills are very limited. Trying to do Communication between two scripts.

chrome.tabs.sendMessage(tab.id, {value: "hello"}, function(response) {
    console.log(response.value);
});

Should communicate with:

chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    if (request.value == "hello")
      sendResponse({value: "goodbye"});
});

However, I am getting the following error:

Cannot read property 'value' of undefined

Why is that and how do I fix it?

Goodbye and thanks in advance

Upvotes: 0

Views: 1957

Answers (1)

ergonaut
ergonaut

Reputation: 7057

At this point in the code, "response" is undefined, so it has no property named "value" (from response.value). So you should probably check if response is undefined before printing it to the console.

Upvotes: 1

Related Questions