簡體   English   中英

反應 js Spring 啟動和 findbyId

[英]React js Spring boot and findbyId

大家好,我需要更新我的 object 但是當我嘗試獲取這個 object 時,我在前端使用 react js 時遇到一些錯誤,代碼是:

class Updateregion extends    Component
{  path= window.location.pathname; 

    constructor(props){
        super(props)

        this.state={
            listeupdate : []
          }
    }
    componentDidMount(){
        axios.get("http://localhost:8080/regions/regionid/"+this.path.substring(14))
        .then(response => response.data)
        .then((data) =>{
          this.setState({listeupdate : data})

        })



      }
    render(){
      const {listeupdate} = this.props;



           return(

        <div> 

            <Menu/>

            {this.state.listeupdate.map((listeupdate)=>(



             {listeupdate.name}

            ))
          }

代碼后端:

@GetMapping("/regionid/{id}")
    public Optional<Region> getRegionId(@PathVariable Long id){

        return rp.findById(id);

    }

我的錯誤是: TypeError: this.state.listeupdate.map is not a function 請問我應該怎么做才能獲取我的數據

您的 API 似乎返回單個 object 因為它使用findById 您可以記錄在 axios 的響應中收到的數據是什么來確認這一點。

如果是這樣,如果要保留數組,可以執行以下操作:

this.setState({listeupdate : [data]})

附帶說明一下,請注意 listeupdate 名稱既用作來自 props 的值,又用作 map 迭代中使用的值

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM