UnhandledPromiseRejectionWarning: ReferenceError:

我正在尝试创建一个辅助方法以在同一个控制器中使用:


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 进程。


繁星点点滴滴
浏览 220回答 2
2回答

缥缈止盈

删除this并使其更具可读性:// method declarationconst 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(...)// ortry {  const resolved = await update(req, res);  // consume the resolved value} catch (e) {  // exception handling}

翻过高山走不出你

我通过以下方式解决:const update = async (req, res) => {  // auxiliar method declaration  verifyItemInStock = itemId => {      // code...  }  // ...  // method call  const hasItems = verifyItemInStock(id)}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript