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

125
Visualizações
use variable req.body in mongoose

My Mongoose schema:

var book_listSchema = new mongoose.Schema({
  userId: {
    type: String,
    required: true
  },
  first_name: {
    type: String,
    required: true
  },
  last_name: {
    type: String,
    required: true
  },
  newList: [],
});

In Express I have the post function

router.post('/fileExpress/:title&:author?',...

var list_name = req.body.list;
var title = req.params.title;
var author = req.params.author;
 addToList[list_name] = [{book_name: title, book_author:author, date_added: new Date()}];

I've added this array to newList through mongoose push command.

 book_list.findOneAndUpdate({"userId": req.userContext.userinfo.sub},{ $push:{newList: addToList}},

This sends data to MongoDB that looks like this:

newList:Array
     0:Object
      read:Array
        0:Object
         book_name:"A Game of Thrones"
         book_author:"George R. R. Martin"
         date_added:2022-04-14T21:23:59.815+00:00

Inside this post function I have the Mongoose command

book_list.findOne({"userId":req.userContext.userinfo.sub,"newList.list_name.book_name":title

The code section newList.list_name.book_name does not work with the variable list_name. It does work when I type in the name of the actual list. In this case 'list_name' is 'read'.

I am using this because there will be lists with many different names and one function needs to work for all of them.

about 4 years ago · Juan Pablo Isaza
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