最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

UnhandledPromiseRejectionWarning:ReferenceError:未定义verifyItemInStock

运维笔记admin10浏览0评论

UnhandledPromiseRejectionWarning:ReferenceError:未定义verifyItemInStock

UnhandledPromiseRejectionWarning:ReferenceError:未定义verifyItemInStock

我正在尝试创建在同一控制器内使用的辅助方法:

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未定义更新时(/home/netogerbi/workspaces/zombieresistance/zombieresistance/app/controllers/trade.controller.js:34:5)(节点:31904)UnhandledPromiseRejectionWarning:未处理的Promise拒绝。该错误是由抛出异步内部引起的没有捕获块或拒绝承诺未使用.catch()处理。 (拒绝ID:1)(节点:31904)[DEP0018]DeprecationWarning:已弃用未处理的承诺拒绝。在未来,未处理的承诺拒绝将终止具有非零退出代码的Node.js进程。

回答如下:

删除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
}
发布评论

评论列表(0)

  1. 暂无评论