Search code examples
reactjsnullresponseis-empty

How do I get the empty value in JSON response?


consider the code below:

fetch(`http://localhost:9000/api/item/list?searchText=${search}`,)
  .then((resp)=>{
   resp.json().then((res)=>{
   if(res.data === 0){
      alert("Sorry, your item cannot be found")
    }else{
      console.log(res.data);
      this.setState({data: res.data});}
 }

Here is my response from console log:

enter image description here

So when data is empty, the alert message will show out. I have try for null but also same. The alert message cannot be show. is it anything I'm wrong?


Solution

  • Instead of res.data === 0

    Try !res.data.length or res.data.length === 0