简体   繁体   English

Axios不会捕获数据并将其发送到客户端

[英]Axios does not catch and send data to client side

I built a backend in Express to fetch Instagram feed images then send the images URL to my front end built with ReactJs. 我在Express中构建了一个后端,以获取Instagram提要图像,然后将图像URL发送到使用ReactJs构建的前端。 If I use instagram-node to fetch the images URL then send it to front end then it everything works, but if I do it manually with axios, it would not work. 如果我使用instagram-node来获取图像URL,然后将其发送到前端,则一切正常,但是如果我使用axios手动进行操作,则它将无法工作。

This is my code with instagram-node npm packet: 这是我的带有instagram-node npm数据包的代码:

ig.user_media_recent('user_id', function(err, medias, pagination, remaining, limit) {
    if(err) console.log(err);
    else{
      var inst = [];
      medias.map(function(content){ inst.push(content.images.thumbnail.url)});
      res.send(inst);
    }
  });

If I do the above then the backend will get the URL and then send it to my front end, but if I do like below with axios 如果我执行上述操作,则后端将获取该URL,然后将其发送至我的前端,但是如果我像下面那样使用axios

axios.get('https://api.instagram.com/v1/users/self/media/recent/?access_token=token')
       .then(function(res){
         console.log(res.data.data[0].images.standard_resolution.url);
         res.send(res.data.data[0].images.standard_resolution.url);
       }).catch(function (err){
         res.send(err.body);
       })

then it would not send anything. 那么它不会发送任何东西。 The console.log still print out the correct URL but the res.send would not do anything. console.log仍然会打印出正确的URL,但是res.send不会执行任何操作。 It would not even show the URL when I go to my backend localhost. 当我进入后端本地主机时,它甚至不会显示URL。 Any idea why? 知道为什么吗?

It seems res.send(res.data.data[0].images.standard_resolution.url) is having some issue here. 似乎res.send(res.data.data[0].images.standard_resolution.url)在这里有问题。 This is how normally we do such things in react. 这就是我们通常在反应中做这些事情的方式。 After the completion of the promise you need to fire an action with the response payload, then a reducer will return the new state. 完成承诺后,您需要使用响应有效负载触发操作,然后化简器将返回新状态。 Finally the UI will get re-rendered with updated data. 最后,UI将使用更新的数据重新呈现。 That's how react works. 这就是反应的方式。

axios.get('https://api.instagram.com/v1/users/self/media/recent/?access_token=token')
       .then(function(res){
         console.log(res.data.data[0].images.standard_resolution.url);
         return { 
            type: GET_INSTA_FEEDS, 
            payload: response.data 
        }
       }).catch(function (err){
         res.send(err.body);
       })

Then write a reducer to get this data and return a new state properly. 然后编写一个化reducer以获取此数据并正确返回新状态。

// ------------------------------------
// Reducer
// ------------------------------------
export const initialState = {
    instaFeeds: []
}

[GET_INSTA_FEEDS]: (state, action) => {
    return { ...state, instaFeeds: action.payload }
}

Bind that state to your component via connect HOC. 通过连接HOC将该状态绑定到您的组件。 That's it 而已

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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