PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, October 28, 2022

[FIXED] How do I get the empty value in JSON response?

 October 28, 2022     is-empty, null, reactjs, response     No comments   

Issue

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



Answered By - k1r4n
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing