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

126
Visualizações
Adding one object to the property of other objects by spread operator in node js giving unnecessary properties?

My First Object is

conversation = {
  "members": [
    "613aa457e8d5f249922e7f86",
    "613aa457e8d5f249922e7faa"
  ],
  "_id": "613aace085dee060fdeb6a9c",
}

My Second object is

message = {
  "read": false,
  "_id": "613aadd58becf96394001efa",
  "sender": "613aa457e8d5f249922e7f86",
  "text": "hi",
  "conversation_id": "613aadcc8becf96394001ee8",
}

My Expected Result is

{
  "members": [
    "613aa457e8d5f249922e7f86",
    "613aa457e8d5f249922e7faa"
  ],
  "_id": "613aace085dee060fdeb6a9c",
  "message": {
    "read": false,
    "_id": "613aadd58becf96394001efa",
    "sender": "613aa457e8d5f249922e7f86",
    "text": "hi",
    "conversation_id": "613aadcc8becf96394001ee8",
  }
}

At First I tried with spread operator by following this link

let new_object= { ...conversation, messages }

and i got the result something like this

enter image description here

I also tried with these way by following stack-overflow

let new_object= Object.assign(conversation, message);

let new_object= Object.assign({},conversation, message);

But I don't get my expected result.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It looks like you might be loading your data from mongoose, which by default is going to decorate your objects such that they aren't the "plain old javascript objects" you think they are.

Mongoose provides a toObject method on Document that can convert an object you loaded from the DB to the plain object you're expecting. You probably want to do something like this:

  // assume conversationFromDb and messageFromDb are Mongoose documents
  const conversation = conversationFromDb.toObject();
  const message = messageFromDb.toObject();

  const newObject = { ...conversation, message };
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