Max Muhstermann Max Muhstermann - 6 days ago 8
Javascript Question

Cannot use 'in' operator to search for 'error' in null

TypeError: Cannot use 'in' operator to search for 'error' in null


I dont know why this error appears. It occures on
'error' in json


function parse(data, cb) {
try {
var json = $.parseJSON(data);
if ('error' in json) {
error(json.error);
return;
}
cb(json);
} catch(err) {
console.log(err);
error();
return;
}
}

Answer

Because $.parseJSON() is returning null. If you're trying to convert a json string into an object, use JSON.parse() : https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse

Comments