Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

461
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda