Null Check In The Return Statement
When I have a null value in data.req, data.req.toLowerCase() is throwing an error. How can I check for a condition like if(data.req === null) { //do nothing } else { data
Solution 1:
You could use a default value, if the data.req
is null
.
(data.req || '').toLowerCase().indexOf(searchTerms.req) !== -1
Or with ES6
(data.req || '').toLowerCase().includes(searchTerms.req)
Solution 2:
Try this:
return data.client.toLowerCase().indexOf(searchTerms.client) !== -1
&& ((data.req == null && (searchTerms.req == null || searchTerms.req == ""))
|| (data.req && data.req.toLowerCase().indexOf(searchTerms.req) !== -1));
Solution 3:
You can simply do following:
return !searchTerm.req || data.client.toLowerCase().indexOf(searchTerms.client) !== -1
&& data.req && data.req.toLowerCase().indexOf(searchTerms.req) !== -1;
Solution 4:
Need to return the value based on condition, issue is resolved. I found the solution. Thanks everyone for your efforts.
return data.client.toLowerCase().indexOf(searchTerms.client) !== -1
&& (searchTerms.req ? data.req && data.req.toLowerCase().indexOf(searchTerms.req) !== -1 :
(data.req == null || data.req.toLowerCase().indexOf(searchTerms.req) !== -1));
// This is for who has de-reputed this question: If possible try to give a solution when someone is merely looking for it.
Post a Comment for "Null Check In The Return Statement"