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

465
Views
How to save multiple items in mongodb using node js

I'm trying to add multiple list in mongodb, but here i'm getting error called .insert(roles) is not a function.i don't understand the issue here thanks in advance

schema:-

module.exports = mongoose => {
const Role = mongoose.model(
  "role",
  mongoose.Schema(
    {
     roleType : [{ type:String }] 
    }
  )
);
return Role;
};

api call:-

  exports.addRoleFields = ( req , res ) => {
      const {  role } = req.body
      const roles =  new Role([{ roleType:  role }]);
      roles
       .insert(roles)
       .then(data => {
         res.status(200).send({data,statusCode:"200"});
       })
       .catch(err => {
         res.status(500).send({
          message: err.message || "Some error occurred while creating.",
          statusCode:"500"
      });
    })
   })    
  }

here is the postman: enter image description here

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Try this:

exports.addRoleFields = (req, res) => {
    const roles = req.body; // const { role } assignment wasn't correct
 
    Role
        .insertMany(roles)
        .then(data => {
            res.status(200).send({ data, statusCode: "200" });
        })
        .catch(err => {
            res.status(500).send({
                message: err.message || "Some error occurred while creating.",
                statusCode: "500"
            });
        })
})    
}
over 4 years ago · Santiago Trujillo Report
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!