EN VI

Node.js - cannot read properties of undefined (reading 'voto'), and others properties?

2024-03-10 03:30:04
How to Node.js - cannot read properties of undefined (reading 'voto'), and others properties

my asynchronous function that basically works when a high prestige user evaluates a question from a common user, he gives an upvote or downvote, if the question has a -1 vote, the common user loses prestige, but it gives an error that I will show after the codes .

my service(question - rabbitmq):

async usuarioPrestigiadoAtualiza(idUsuario: string, idUsuarioPropio: string, idPergunta: string,   dados: atualizaEspecificoDTO) {
    
    
    const usuario = await this.usuarioMetaRepository.findOne({where: {id: idUsuario}})
    if(usuario === null){
      throw new NotFoundException('Usuario Prestigiado nao existe')
    }
    const usuario2 = await this.usuarioMetaRepository.findOne({where: {id: idUsuarioPropio}})
    if(usuario2 === null){
      throw new NotFoundException('Usuario Propio nao existe')
    }
    // if(usuario2.pergunta.id !== perguntaAchada.id){
    //   throw new UnauthorizedException('Usuario Propio nao possui essa pergunta')
    // }
    const perguntaAchada = await this.perguntaRepository.findOne({where: {id: idPergunta}})
    if(perguntaAchada === null){
      throw new NotFoundException('Pergunta nao existe')
    }
    
    
    if(usuario.prestigio < 100){
      throw new UnauthorizedException(`Usuario: ${usuario.nome} nao possui prestigio suficiente!`)
    }
    if (dados.voto === -1) {
      usuario2.prestigio -= 5;
      usuario2.voteForce -= 2;
    } else if (dados.voto >= 1) {
      for (let i = 0; i < 100; i++) {
        if (usuario2.pergunta.voto + 1) {
          usuario2.prestigio += 4;
          usuario2.voteForce += 1;
        }
      }
    }
    if (usuario.voteForce > 30) {
      dados.voto *= 2;
    }
    if (usuario.voteForce > 60) {
      dados.voto *= 3;
    }
    if (usuario.voteForce >= 100) {
      dados.voto *= 4;
    }
    if(usuario2.prestigio >= 100){
      usuario2.prestigio === 100
    }
    // if(dados.voto >= -10){
    //   return await this.perguntaRepository.remove(perguntaAchada)
    // }

    perguntaAchada.id = dados.id
    perguntaAchada.titulo = dados.titulo
    perguntaAchada.voto = dados.voto
    perguntaAchada.pergunta = dados.pergunta
    const perguntaMudadaPorPrestigiado = await this.perguntaRepository.save(perguntaAchada)
    console.log(perguntaMudadaPorPrestigiado)
    return perguntaMudadaPorPrestigiado

  }

my controler if need(rabbitmq)

 @MessagePattern({ cmd: 'put-perguntaPrestigio'})
  async enviarPerguntaAvaliada(@Ctx() contexto: RmqContext, @Payload() payload: { idUsuario: string; idUsuarioPropio: string; idPergunta: string; dados: atualizaEspecificoDTO }){
    const channel = contexto.getChannelRef()
    const message = contexto.getMessage()
    channel.ack(message)

    return this.perguntaService.usuarioPrestigiadoAtualiza(payload.idUsuario, payload.idUsuarioPropio, payload.idPergunta, payload.dados )

api gateway controler:

@Put('usuarioM/:id1/:id2/:id3')
  async perguntaAtualizaPorPrestigiado(@Param('id1') idUsuario: string, @Param('id2') idUsuarioPropio: string, @Param('id3') idPergunta: string,  @Body() data: atualizaEspecificoDTO) {
    return this.authServico.send(
      {
        cmd: 'put-perguntaPrestigio'
      }, {idUsuario, idUsuarioPropio, idPergunta, data}
    )
  }

As I said, I need a high-prestige user to evaluate a common user, but when I try to post the user's question, it says:

ERROR [RpcExceptionsHandler] Cannot read properties of undefined (reading 'voto')

when I remove vote, it happens with id, if I remove id, it happens with the title and so on, all the properties of the DTO, I was using an entity but I preferred to update the question found (perguntaAchada), as if it didn't get the information from postman, I already tried logging and nothing came out.

Solution:

You aren't sending an object with a dados property to your microservice from the gateway controller. The object's properties are idUsuario, idUsuarioPropio, idPergunta, and data. It looks like you are expecting dados to be the data property, and if that's the case then you should update your types accordingly

Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login