Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

128
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda