Home > database >  UnhandledPromiseRejectionWarning: Unhandled promise rejection. - Get this error when I try and load
UnhandledPromiseRejectionWarning: Unhandled promise rejection. - Get this error when I try and load

Time:06-22

My server was working previously when I did not have a catch block in my server side code, now that I have added a catch block it throws this error:

(TypeError: Cannot read property 'catch' of undefined)

followed by this error: (UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client)

and this error: (UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch().)

Server side code:

const axios = require('axios');
const router = require('express').Router();

router.get('/mavenlink', (req, res) => {
  const headers = {
    Authorization: `Bearer ${req.query.token}`,
  };
    axios({url:"https://api.mavenlink.com/api/v1/workspaces.json", headers: headers})
    .then((response) => res.send(response.data))
    console.log(req.query)
    .catch((error) => {
      console.log(error);
    });
})


module.exports = router;

client side code:


    const getData = () => {
        axios({
            method: 'get',
            url: 'http://localhost:5000/mavenlink?token=' accessToken,
            data: {}
        })
        .then((response) => {
            setApiData(response.data.workspaces)
            console.log(apiData);
        })
        .catch((error) => {
            console.log(error);
            setErrorAlert(true)
        });
    }

The weird thing is this was working fine previously when I did not have a catch block in the server side code, and I was pulling in the data fine. However, when I went onto my server at localhost:5000/mavenlink it would crash the server and throw only this error: ( UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch().). This is what led me to adding a catch block which then led to the 2 additional errors described earlier in this post.

CodePudding user response:

you are trying to attach the .catch to console.log making it throw a exception

router.get('/mavenlink', (req, res) => {
  console.log(req.query)
  const headers = {
    Authorization: `Bearer ${req.query.token}`,
  };
    axios({url:"https://api.mavenlink.com/api/v1/workspaces.json", headers: headers})
    .then((response) => res.send(response.data))
    .catch((error) => {
      console.log(error);
    });
})

CodePudding user response:

You have added console.log() statement in promise chain in server side code, line after then. Remove than and issue will be resolved.

Fixed code:

const axios = require('axios');
const router = require('express').Router();

router.get('/mavenlink', (req, res) => {
    const headers = {
        Authorization: `Bearer ${req.query.token}`,
    };
    console.log(req.query)
    axios({url:"https://api.mavenlink.com/api/v1/workspaces.json", headers: headers})
    .then((response) => res.send(response.data))
    .catch((error) => {
        console.log(error);
    });
})


module.exports = router;
  • Related