为了账号安全,请及时绑定邮箱和手机立即绑定

UnhandledPromiseRejectionWarning: ReferenceError:

UnhandledPromiseRejectionWarning: ReferenceError:

繁星点点滴滴 2021-11-18 20:40:32
我正在尝试创建一个辅助方法以在同一个控制器中使用:module.exports = {  async update(req, res) {    // code here...    // method call    this.verifyItemInStock()    // more code here ...  },  // method declaration  verifyItemInStock (itemId) {      // more code...  }}但我收到以下错误:(node:31904) UnhandledPromiseRejectionWarning: ReferenceError: verifyItemInStock is not defined at update (/home/netogerbi/workspaces/zombieresistance/zombieresistance/app/controllers/trade.controller.js:34:5) (node:31904) UnhandledPromiseRejection promiseWarning: Unhandled拒绝。这个错误要么是因为在没有 catch 块的情况下抛出了异步函数,要么是因为拒绝了一个没有用 .catch() 处理过的承诺。(rejection id: 1) (node:31904) [DEP0018] DeprecationWarning:不推荐使用未处理的承诺拒绝。将来,未处理的承诺拒绝将使用非零退出代码终止 Node.js 进程。
查看完整描述

2 回答

?
缥缈止盈

TA贡献2041条经验 获得超4个赞

删除this并使其更具可读性:


// method declaration

const verifyItemInStock = itemId => {

  // more code...

}


const update = async (req, res) => {

  // code here...

  // method call

  verifyItemInStock()

  // more code here ...

}



module.exports = {

  update,

  verifyItemInStock,

}

此外,承诺的消费者应该有一个问题:


import { update } from './my-module';


update(req, res).then(...).catch(...)

// or

try {

  const resolved = await update(req, res);

  // consume the resolved value

} catch (e) {

  // exception handling

}


查看完整回答
反对 回复 2021-11-18
?
翻过高山走不出你

TA贡献1875条经验 获得超3个赞

我通过以下方式解决:


const update = async (req, res) => {


  // auxiliar method declaration

  verifyItemInStock = itemId => {

      // code...

  }


  // ...

  // method call

  const hasItems = verifyItemInStock(id)


}


查看完整回答
反对 回复 2021-11-18
  • 2 回答
  • 0 关注
  • 219 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信