Skip to content
Advertisement

How @Inject(REQUEST) works and how to destructure it

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.

Advertisement

Answer

As described by this question, JavaScript is generally pass-by-value except for objects which are passed by reference. Due to this, if an variable is assigned to an object, it can see changes to that object later on, but if it is assigned to a primitive somehow (property of an object in this case) then it will not see when that property gets updated, because it is passed by value only. You can see this with a short snippet here (run it in a node REPL or the browser)

const req = { foo: 'foo', fooBar: { foo: 'foo', bar: 'bar' } }
const bar = req.bar; // undefined passed by value
const fooBar = req.fooBar; // object passed by reference
const fooBarFoo = req.fooBar.foo; // undefined passed by value
req.bar = 'bar'
req.fooBar.fooBar = 'fooBar'
req.fooBar.foo = 'not foo'
console.log(req) // original object
console.log(bar) // still undefined
console.log(fooBar) // full object, with fooBar property and "not foo" for foo property
console.log(fooBarFoo) // still "foo"

This explains why in the constructor req.user (and subsequent properties) is undefined, but in the method it works fine.

What you can do, to be able to use this.user is create a getter for your class like

export class AppService {
  constructor(@Inject(REQUEST) private request) {}

  get user() {
    return this.request.user.userId
  }

  myMethod() {
    console.log(this.user); // userId exists
  }
}
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement