Javascript-无法读取未定义的属性 [英] Javascript - cannot read property of undefined

查看:50
本文介绍了Javascript-无法读取未定义的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以向我解释为什么我会收到值的属性是未定义的错误"吗?我的Javascript技能非常有限.尝试在两个脚本之间进行通信.

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"});
});

但是,出现以下错误:

无法读取未定义的属性值"

Cannot read property 'value' of undefined

为什么会这样,我该如何解决?

Why is that and how do I fix it?

再见,谢谢!

推荐答案

在代码中,"response"尚未定义,因此它没有名为"value"的属性(来自response.value).因此,您可能应该在将响应打印到控制台之前检查响应是否未定义.

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.

这篇关于Javascript-无法读取未定义的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆