Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

466
Views
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.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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
  }
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!