【问题标题】:axios expressJS different filesaxios expressJS 不同的文件
【发布时间】:2020-04-23 02:49:33
【问题描述】:

在我看到的每个地方,每个人都在 express get 调用中使用 axios 调用。我不想那样做。我在另一个文件中有 API 调用的代码,并从我的路由器调用它。我只是不知道如何访问返回的数据。有人可以帮忙吗?

axios 调用:

function weather(geo) {
  let geoLocation = `https://api.mapbox.com/geocoding/v5/mapbox.places/${encodeURIComponent(geo)}.json?access_token=pk.eyJ1IjoiY2FydGVydzIxMjQiLCJhIjoiY2s5NGNlcmY2MDAzaTNncDJwN2ozdWk2byJ9._fm9rvVvhbCJE2eN0C-gqQ&limit=1`
  axios.get(geoLocation)
    .then((response) => {
      if (response.data.features.length === 0) {
        return console.log('Unable to find location')
      } else {
        const long = response.data.features[0].center[1]
        const lat = response.data.features[0].center[0]
        const url = `http://api.weatherstack.com/current?access_key=ebe5b5662c85c2740c00d538723b44b3&query=${long},${lat}&units=f`
        return axios.get(url)
      }
    })
    .then((response) => {
      if (response.data.error) {
        console.log(response.data.error.info)
      } else {
        console.log(response.data)
        let forecastData = response.data;
        return forecastData
      }
    })
    .catch((error) => {
      console.log('Unable to connect to weather service')
    })
}

特快电话:

router.get('/help', function (req, res) {
  weather.weather(req.query.address)
  res.send({
    address: req.query.address,
    forecast: '?'
  })
})

【问题讨论】:

    标签: node.js express axios


    【解决方案1】:

    您的weather 函数是异步的,因此您应该为axios 调用添加返回值:

    function weather(geo) {
    
      let geoLocation = `https://api.mapbox.com/geocoding/v5/mapbox.places/${encodeURIComponent(geo)}.json?access_token=pk.eyJ1IjoiY2FydGVydzIxMjQiLCJhIjoiY2s5NGNlcmY2MDAzaTNncDJwN2ozdWk2byJ9._fm9rvVvhbCJE2eN0C-gqQ&limit=1`
      return axios.get(geoLocation)
        .then((response) => {
          if (response.data.features.length === 0) {
            throw 'Unable to find location'
          } 
          else {
            const long = response.data.features[0].center[1]
            const lat = response.data.features[0].center[0]
            const url = `http://api.weatherstack.com/current?access_key=ebe5b5662c85c2740c00d538723b44b3&query=${long},${lat}&units=f`
            return axios.get(url)
          }
        })
        .then((response) => {
          if (response.data.error) {
            throw response.data.error.info
          } 
          else {
            console.log(response.data)
            let forecastData = response.data;
            return forecastData
          }
        })
        .catch((error) => {
          throw error;
        })
    }
    

    在您的路由器中:

    router.get('/help', function (req, res) {
      weather.weather(req.query.address)
        .then(res => {
          res.send({
            address: req.query.address,
            forecast: '?'
          })
        })
        .catch(err => {
          console.log(err);
        })
    
    })
    

    同样返回console.log 也不正确。只需抛出它的消息并在路由器中处理消息:

    来自

    if (response.data.features.length === 0) {
      return console.log('Unable to find location')
    }
    

    if (response.data.features.length === 0) {
      throw 'Unable to find location'
    }
    

    【讨论】:

    • 感谢赛义德的帮助
    猜你喜欢
    • 1970-01-01
    • 2021-10-27
    • 2019-05-19
    • 2019-06-01
    • 2019-08-18
    • 2018-09-13
    • 2018-12-28
    • 2016-05-05
    • 1970-01-01
    相关资源
    最近更新 更多