简体   繁体   English

Node.js 未捕获此模块的错误

[英]Node.js Is Not Catching This module's Error

here is module这是模块

const imgurUser = require('imgur-user');

and this module when did not found an imgur user throws error.这个模块在没有找到 imgur 用户时会抛出错误。 but I cant Catch the error:但我无法捕捉到错误:

try{
    imgurUser('dsfksdkfsdf4').then(res => {
    console.log(res);
        
    })
}catch(err){
    console.log("error")
}

I think maybe what'll solve your problem is to put a catch block after the.then:我想也许可以解决你的问题是在.then之后放置一个catch块:

imgurUser('user')
  .then(res => {
    console.log(res);
  })
.catch(err => {
  console.error(err);
})

If you want to, you could use async/await.如果你愿意,你可以使用 async/await。 Like this:像这样:

const fetchImgurUser = async (user) => {
  try {
    const res = await imgurUser(user);
  } catch (err) {
    console.error(err);
  }
}

fetchImgurUser('user');

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

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