通过此关键字调用函数时发生TypeError

时间:2018-11-04 09:16:24

标签: 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)

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

1 个答案:

答案 0 :(得分:1)

Express中间件功能中的

this is server instance。没有理由将failed称为this.failed

如果在此中间件功能的范围内可用,则应将其称为failed

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