2017-04-17 7 views
0

プロパティが存在するかどうかをオブジェクトで評価するたびに、次のエラーメッセージが表示されます。Error: undefined is not an object (evaluating 'a.result.hasOwnProperty')エラー:オブジェクトのプロパティが存在するかどうかを確認するときに、未定義のオブジェクトは存在しません。

どうすれば修正できますか?

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); 
    } 
+0

でなければなりません。それが正しいことを確認してください。 – dfsq

+0

'a.result'の値は何ですか? –

+0

'a.result.hasOwnProperty'をチェックする前に、' a.result'をチェックする必要がありますか? – methuselah

答えて

1

あなたの条件だからa.resultがオブジェクトではありません

(a && a.result && a.result.hasOwnProperty('rawMisMatchPercentage') && (b && 
b.result && b.result.hasOwnProperty('rawMisMatchPercentage')) 
関連する問題