简体   繁体   中英

How to get nodejs api data in reacj js in axiuos or fetch api

I have a question regarding zoom API. How to fetch nodeJs API data in reacjJs in axiuos or fetch API,

I am trying to fetch data from nodeJs in reactJs, but I am getting a null response.

My code is below.

Node.js:

router.get('/auth', function(req, res, next) {
if (req.query.code) {

    let url = 'https://zoom.us/oauth/token?grant_type=authorization_code&code=' + req.query.code + '&redirect_uri=' + process.env.redirectURL;

    request.post(url, (error, response, body) => {

        // Parse response to JSON
        body = JSON.parse(body);
 
        console.log(`access_token: ${body.access_token}`);
        console.log(`refresh_token: ${body.refresh_token}`);

        if (body.access_token) {

            request.get('https://api.zoom.us/v2/users/me', (error, response, body) => {
                if (error) {
                    console.log('API Response Error: ', error)
                } else {
                    body = JSON.parse(body);
               
                    console.log('API call ', body);
                
                    var JSONResponse = '<pre><code>' + JSON.stringify(body, null, 2) + '</code></pre>'
                    res.send(`
                        <style>
                            
                        </style>
                      
                                    <h2>${body.first_name} ${body.last_name}</h2>
                                    <p>${body.role_name}, ${body.company}</p>
                               

                                ${JSONResponse}
                    
                    `);
                }
            }).auth(null, null, true, body.access_token);

        } else {
            // Handle errors, something's gone wrong!
        }

    }).auth(process.env.clientID, process.env.clientSecret);

    return;

  }

React.js:

componentDidMount() {

const url = 'http://localhost:3000/api';
      axios.post(
          url,
          {
              path: '/v2/users/info@data.nyc',
              methode: 'GET'
          }

      )
          .then((response) => {
                  console.log(response)
              },
              (error) => {
                  console.log(error)
              }
          );
}

Your axios call is incorrect. Try below.

componentDidMount() {
  const url = 'http://localhost:3000/api';
  axios
    .get(url + '/v2/users/info@data.nyc')
    .then(function (response) {
      // handle success
      console.log(response);
    })
    .catch(function (error) {
      // handle error
      console.log(error);
    });
}

i suggest u to fix the axios page:

try this:

const url = 'http://localhost:3000/api';
const zoom = () => {
  Axios.get(url + '/v2/users/info@data.nyc')
    .then((response) => {
    console.log(response)
  }) 
};

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