javascript - 通过此关键字调用函数时出现 TypeError

标签 javascript node.js

我想通过这个关键字调用一个文件中的函数,但我得到一个 TypeError。 以下是我在一个文件中的部分代码:

router.get('/products', function (req, res){
  try {
    let page = req.query.page || 1;
    let products = Product.paginate({}, { page, sort: { createdAt: 1 }, limit: 12, populate: [{ path: 'categories' }, { path: 'user' }] });
    res.json({
      data: this.filterProductsData(products),
      status: 'success'
    })
  } catch (err) {
    this.failed(err.message, res);
  }
})

function failed(msg , res , statusCode = 500) {
  res.status(statusCode).json({
      data : msg,
      status : 'error'
  })
}

错误文本是:

TypeError: this.failed is not a function
    at C:\Users\Sayyid Ali\Desktop\gheymat\app\routes\v1\home.js:27:10
    at Layer.handle [as handle_request] (C:\Users\Sayyid Ali\Desktop\gheymat\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Users\Sayyid Ali\Desktop\gheymat\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (C:\Users\Sayyid Ali\Desktop\gheymat\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\Users\Sayyid Ali\Desktop\gheymat\node_modules\express\lib\router\layer.js:95:5)
    at C:\Users\Sayyid Ali\Desktop\gheymat\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (C:\Users\Sayyid Ali\Desktop\gheymat\node_modules\express\lib\router\index.js:335:12)
    at next (C:\Users\Sayyid Ali\Desktop\gheymat\node_modules\express\lib\router\index.js:275:10)
    at Function.handle (C:\Users\Sayyid Ali\Desktop\gheymat\node_modules\express\lib\router\index.js:174:3)
    at router (C:\Users\Sayyid Ali\Desktop\gheymat\node_modules\express\lib\router\index.js:47:12)
    at Layer.handle [as handle_request] (C:\Users\Sayyid Ali\Desktop\gheymat\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (C:\Users\Sayyid Ali\Desktop\gheymat\node_modules\express\lib\router\index.js:317:13)
    at C:\Users\Sayyid Ali\Desktop\gheymat\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (C:\Users\Sayyid Ali\Desktop\gheymat\node_modules\express\lib\router\index.js:335:12)
    at next (C:\Users\Sayyid Ali\Desktop\gheymat\node_modules\express\lib\router\index.js:275:10)
    at cors (C:\Users\Sayyid Ali\Desktop\gheymat\node_modules\cors\lib\index.js:188:7)
    at C:\Users\Sayyid Ali\Desktop\gheymat\node_modules\cors\lib\index.js:224:17
    at originCallback (C:\Users\Sayyid Ali\Desktop\gheymat\node_modules\cors\lib\index.js:214:15)
    at C:\Users\Sayyid Ali\Desktop\gheymat\node_modules\cors\lib\index.js:219:13
    at optionsCallback (C:\Users\Sayyid Ali\Desktop\gheymat\node_modules\cors\lib\index.js:199:9)
    at corsMiddleware (C:\Users\Sayyid Ali\Desktop\gheymat\node_modules\cors\lib\index.js:204:7)
    at Layer.handle [as handle_request] (C:\Users\Sayyid Ali\Desktop\gheymat\node_modules\express\lib\router\layer.js:95:5)

我该怎么办? 是因为功能正常吗?

最佳答案

this is server instance内部 Express 中间件功能。没有理由将 failed 称为 this.failed

如果它在这个中间件函数的范围内可用,它应该被称为失败:

  ...
  } catch (err) {
    failed(err.message, res);
  }
  ...

关于javascript - 通过此关键字调用函数时出现 TypeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53139272/

相关文章:

javascript - 展开/折叠切换不会随着内容展开而向下 float

javascript - 反斜杠 - 正则表达式 - Javascript

node.js - use_env_variable 在 sequelize 中返回 undefined

node.js - 有没有办法在 Node.js 中调用 module.require 上的resolve()?

javascript - 更改页面背景。本地存储

javascript - 在 IE9 上使用 DISPID_NEWENUM 使用 C++ 检索 javascript 数组对象失败

javascript - 从 jQuery 中的数量输入框计算价格

javascript - 使用 Node.js 库从平衡支付中获取 JSON 数据

node.js - ES6 + jasny-bootstrap - 错误 : Cannot find module jasny-bootstrap?

javascript - 如何编写面向对象的 Node.js 模型