简体   繁体   English

类型错误:无法读取 null 的属性

[英]TypeError: Cannot read property of null

I get the error in the title even though I have tried all the solutions.即使我已经尝试了所有解决方案,我仍然收到标题中的错误。 About the value of null;关于null的值;

If my code is ;

Output: TypeError: Cannot read property of null


  
 
  
  
  
case '!facebook':
face.Profile(facebook, function(result) {

let like = result.profiles.like;
let comments = result.profiles.comments;
        							
if(like === null || comments === null){
//code.
}
else {
//code.
}
});
break;

You have to verify that each child of your object exists before you reference the next level.在引用下一个级别之前,您必须验证对象的每个子项是否存在。 In your case, that means testing the existence of result and then result.profiles before trying to use any of the object values in profiles .在您的情况下,这意味着在尝试使用profiles任何对象值之前先测试resultresult.profiles的存在。 See the code below.请参阅下面的代码。

Without seeing the rest of your case statement or how you're setting the value of face , it's hard to tell if you're going to run into other issues.如果没有看到您的case语句的其余部分或您如何设置face的值,很难判断您是否会遇到其他问题。

 case '!facebook': face.Profile(facebook, function(result) { // Set default values let like = null; let comments = null; // Set values only if there's a result containing profiles if (result && result.profiles) { like = result.profiles.like; comments = result.profiles.comments; } if (like === null || comments === null){ //code } else { //code } }); break;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM