The first question is how it's possible that an injected parameter is changing after the class initialization. Because this code works
export class AppService {
constructor(@Inject(REQUEST) private request) {}
myMethod() {
console.log(this.request.user.userId); // userId exists
}
}
and this code doesn't work
export class AppService {
private user;
constructor(@Inject(REQUEST) request) {
console.log(request) // it has request but no user in it
this.user = request?.user?.userId;
}
myMethod() {
console.log(this.user); // undefined
}
}
The second example doesn't have 'userId' yet that should be injected by my nest.js interceptor. Why it's like that if both of those code snippets are identical? Even if the request variable only points at object, why it doesn't work in second scenario? All I want is to destructure request object to get userId from it.