I want to fetch some data from an API on some conditions. Below is my code:
const readmsg = async (a) => {
((a.receiver === me) & (a.is_read === false)) ?
let data1 = new FormData()
data1.append('is_read',true)
await chatApi.post(`updateMsg/${a.id}/`,data1)
:
null
}
But this giving me syntax errors. How can I write this correctly? Even I tried if and else instead of ternery operator, that does not gives syntax error but throws error.
Use an ordinary if statement.
const readmsg = async (a) => {
if (a.receiver === me && !a.is_read) {
let data1 = new FormData();
data1.append('is_read','true');
return await chatApi.post(`updateMsg/${a.id}/`,data1);
} else {
return null;
}
};
Additionally:
&& to combine conditions; & is bit-wise AND.FormData.append() should be a string or blob, not a boolean. Put true in quotes to make it a string.=== when testing boolean values. Just use the value or !value.