简体   繁体   English

错误:检查对象的属性是否存在时,undefined不是对象

[英]Error: undefined is not an object when checking to see if property of object exists

Whenever I evaluate my object to see if a property exists, I keep getting the following error message: Error: undefined is not an object (evaluating 'a.result.hasOwnProperty') . 每当我评估对象以查看属性是否存在时,我都会不断收到以下错误消息: Error: undefined is not an object (evaluating 'a.result.hasOwnProperty')

How can I fix it? 我该如何解决?

    if(++responseCount === products.length) {
      products.sort(function(a, b) {
        if((a.result.hasOwnProperty('rawMisMatchPercentage')) && (b.result.hasOwnProperty('rawMisMatchPercentage'))) {
            return a.result.rawMisMatchPercentage - b.result.rawMisMatchPercentage;
        }
      });
      return products.slice(0, 3);
    }

Your condition should be 你的情况应该是

(a && a.result && a.result.hasOwnProperty('rawMisMatchPercentage') && (b && 
b.result && b.result.hasOwnProperty('rawMisMatchPercentage'))

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

相关问题 Javascript检查对象属性是否存在,即使对象未定义 - Javascript check if object property exists, even when object is undefined 检查对象属性时与未定义或“未定义”的比较。 有什么区别? - Comparison with undefined or “undefined” when checking for object property. What is the difference? 检查可能未定义的对象属性 - Checking object property of potentially undefined 对象属性未定义,但我看到不是 - object property undefined but I see it is not 对象属性不存在时触发错误 - Fire an error when Object property does not exists 检查JavaScript中对象中是否存在属性(浏览器兼容性) - Checking if property exists in object in JavaScript (browser compliance) 在获取JavaScript中的属性之前检查对象存在 - Checking object exists before getting property in JavaScript JSON响应问题,结果对象的属性被读取为未定义,但我可以看到它存在 - Issue with JSON response, property of resultant object is being read as undefined but I can see it exists 检查内联值是否存在,在声明 object _ JavaScript 时设置为 object 属性 - checking inline value is exists, set to object property in declaring object _ JavaScript 在检查属性是否作为对象文字的一部分存在时,HasOwnProperty有什么优势? - What Advantage Does HasOwnProperty Offer When Checking if a Property Exists As Part of An Object Literal?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM