我如何在JSON响应中获取空值?

问题描述 投票:0回答:1

请考虑以下代码:

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});}
 }

这是我从控制台日志中得到的答复:

enter image description here

因此,当数据为空时,将显示警报消息。我尝试过null,但也一样。警报消息无法显示。我有什么不对吗?

reactjs null response is-empty
1个回答
0
投票

而不是res.data === 0

尝试!res.data.length或res.data.length === 0

© www.soinside.com 2019 - 2024. All rights reserved.