I have the javascript below, it works only when there is no null value in the json data output, however when there is a null json output as in
{"id":30,"name":"John Don","email":"[email protected]","latest_payment":null}
i get an error.
Uncaught TypeError: value.latest_payment is null
JQuery.ajax({
type:"GET",
url : '/list',
data:{tenantSelected:tenantSelected},
dataType : "json",
success:function(data)
{
jQuery.each(data, function(key,value){
if(tenantSelected == value.id){
var x=value.latest_payment;
if(x !== null) { //conditional statement to check if null
console.log(value.latest_payment);
} else {
console.log("No Data");
}
}
});
}
});
Here is sample json output.
[{{"id":29,"name":"Jane Doe","email":"[email protected]","latest_payment":{"id":44,"amount":120000,"rent_from":"2020-01-01","rent_to":"2020-03-01","tenant_id":29},},//This returns correct output
{"id":30,"name":"John Don","email":"[email protected]","latest_payment":null}] //returns an error Uncaught TypeError: value.latest_payment is null
And ideas?
2
Answers
Inside the if statement try this
try like this