简体   繁体   中英

response.body is returning null and response.data is returning undefined in my react+express app

App.js in React

  const url = "http://localhost:3001?address="+this.state.location ;
    fetch(url , {
      mode: 'no-cors' // 'cors' by default
    }).then((response)=>{
        console.log(response.data) //undefined
        console.log(response.body) //null
  })
}

app.js in my node app

app.get('/weather' , (req , res)=>{
    if(!req.query.address){
        return res.send({
            error : 'Please, provide an address' // want to get this value in my react app after fetch.
        })
    }
    geoCode(req.query.address , (error , {longitude , latitude , place}={})=>{
        if(error)
        {
            return res.send({
                error // want this value
            })
        }
        getWeather (latitude  ,longitude , (error , {temperature , humidity})=>{
            if(error)
            {
                return res.send({
                   error // want this value
                })
            }
           // want these values
            return res.send({
                temperature ,
                humidity ,
                place
            })


        })
    })

})

I want to get error message or data as sent from my node app in my react app. But reponse.data and response.body is not performing well in my react app.

API call should be.

  const url = "http://localhost:3001?address=" + this.state.location;
  fetch(url, {
    mode: 'no-cors'
  })
  .then(res => res.json())
  .then((response) => {
    console.log(response.data)
    console.log(response.body)
  })

refer here for more info about fetch API call in react -> fetch api call

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM