简体   繁体   English

UnhandledPromiseRejectionWarning:未处理的承诺拒绝。 - 当我尝试在 localhost:5000 上加载服务器时出现此错误

[英]UnhandledPromiseRejectionWarning: Unhandled promise rejection. - Get this error when I try and load up server on localhost:5000

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:当我的服务器端代码中没有 catch 块时,我的服务器之前正在工作,现在我添加了一个 catch 块,它会引发此错误:

(TypeError: Cannot read property 'catch' of undefined) (TypeError:无法读取未定义的属性“catch”)

followed by this error: (UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client)后跟此错误:(UnhandledPromiseRejectionWarning: E​​rror [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().)和这个错误:(UnhandledPromiseRejectionWarning:未处理的承诺拒绝。这个错误源于在没有catch块的异步函数内部抛出,或者拒绝没有用.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.奇怪的是,以前当我在服务器端代码中没有 catch 块时,它工作得很好,而且我在提取数据时做得很好。 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().).但是,当我在 localhost:5000/mavenlink 进入我的服务器时,它会使服务器崩溃并仅抛出此错误:拒绝未使用 .catch() 处理的承诺。)。 This is what led me to adding a catch block which then led to the 2 additional errors described earlier in this post.这就是导致我添加一个 catch 块的原因,然后导致了本文前面描述的 2 个额外错误。

you are trying to attach the .catch to console.log making it throw a exception您正在尝试将.catch附加到console.log使其抛出异常

edit: in case of error you can send it to the error middleware.编辑:如果出现错误,您可以将其发送到错误中间件。 Make sure that your routes have a end or it will block node确保您的路线有终点,否则它将阻塞节点

router.get('/mavenlink', (req, res,next) => {
  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);
      next(error)
    });
})

You have added console.log() statement in promise chain in server side code, line after then.您已经在服务器端代码的承诺链中添加了 console.log() 语句,然后是一行。 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;

暂无
暂无

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

相关问题 为什么会收到UnhandledPromiseRejectionWarning:未处理的承诺被拒绝? - Why I get UnhandledPromiseRejectionWarning: Unhandled promise rejection? UnhandledPromiseRejectionWarning:未处理的承诺拒绝。 在使用 aync 和 await 时,量角器控制台中不显示警告 - UnhandledPromiseRejectionWarning: Unhandled promise rejection. warning is displaued in protractor console on using aync and await UnhandledPromiseRejectionWarning:未处理的 API 承诺拒绝 - UnhandledPromiseRejectionWarning: Unhandled promise rejection for API UnhandledPromiseRejectionWarning:未处理的承诺拒绝 - API - UnhandledPromiseRejectionWarning: Unhandled promise rejection - API 未处理的 promise 拒绝。 此错误源于在没有 catch 块的情况下抛出异步 function - Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block 未处理的承诺被拒绝。 该错误是由抛出异步函数引发的-NodeJS - Unhandled promise rejection. This error originated either by throwing inside of an async function - NodeJS UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝ID:3): - UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 3): UnhandledPromiseRejectionWarning:CRA 通用应用程序上未处理的承诺拒绝错误 - UnhandledPromiseRejectionWarning: Unhandled promise rejection Error on CRA Universal app UnhandledPromiseRejectionWarning:未处理的承诺拒绝错误,我正在使用 Node JS 和 mongo Db - UnhandledPromiseRejectionWarning: Unhandled promise rejection error I am getting I am using Node JS and mongo Db 可能未处理的承诺拒绝。 无法读取未定义的属性 - Possible Unhandled Promise Rejection. Cannot read property of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM