Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

126
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!