在高阶函数(nestjs)中未定义 this
我正在尝试实现一些基本的高阶函数,它将返回一个新函数,其中提供的函数被 try catch 块包围。像这样的事情:
function wrapLogging(f) {
return async (...args) => {
try {
await f(...args);
} catch (e) {
log(e);
};
}
}
// dummy example:
const executeAndLog = wrapLogging(executeFunction);
executeAndLog();
// nestjs controller and service example:
@Post('/create')
async executeAndLog() {
const executeAndLog = wrapLogging(this.exampleService.create);
await executeAndLog();
}
现在的问题是我在提供的 this.exampleService.create 中遇到未定义的错误。 “类型错误:无法读取未定义的属性...” 我确实知道上下文缺失,但不知道如何“连接”它。尝试谷歌搜索示例并发现 .call 或 .apply 解决方案存在类似问题,但在这种情况下不起作用。也许是因为提供的服务方法是异步的,或者与nestjs服务及其上下文有关?
非常感谢任何帮助!蒂
I'm trying to implement some basic higher order function which would return a new function with provided function surrounded with try catch block. Something like this:
function wrapLogging(f) {
return async (...args) => {
try {
await f(...args);
} catch (e) {
log(e);
};
}
}
// dummy example:
const executeAndLog = wrapLogging(executeFunction);
executeAndLog();
// nestjs controller and service example:
@Post('/create')
async executeAndLog() {
const executeAndLog = wrapLogging(this.exampleService.create);
await executeAndLog();
}
Now the problem is that I get undefined errors in provided this.exampleService.create.
"TypeError: Cannot read properties of undefined..."
I do get that the context is missing but don't know how to 'connect' it. Tried googling examples and found similar problem with .call or .apply solutions but it didn't work in this case. Maybe it's because the service method provided is async, or it's something to do with nestjs services and their context?
any help is greatly appreciated! ty
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要使用这样的方法,您需要认识到在使用
函数
时词法this
会丢失。您可以使用.bind()
将正确的this
重新绑定到该方法,以便您仍然可以在该方法内使用this
。以下内容可以保留this
但允许您包装方法app.controller.ts
app.service.ts
HTTP 调用
日志
我建议采用装饰器的路线并修改
描述符。 value
而不是单独的函数,因为每次您想要包装方法时都需要执行此bind
操作,但最终取决于您。To use an approach like this, you need to recognize that the lexical
this
gets lost when usingfunction
s. You can use.bind()
to rebind the properthis
to the method, so that you can still usethis
inside the method. The following works in keepingthis
but allowing you to wrap your methodsapp.controller.ts
app.service.ts
HTTP call
logs
I would suggest going the route of a decorator and modifying the
descriptor.value
instead of a separate function, as you'll need to do thisbind
every time you want to wrap your method, but in the end it's up to you.