[英]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.