gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 20:03:33 25 4
gpt4 key购买 nike

我想通过这个关键字调用一个文件中的函数,但我得到一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com