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

457
Views
Cómo guardar varios elementos en mongodb usando el nodo js

Estoy tratando de agregar varias listas en mongodb, pero aquí recibo un error llamado .insert(roles) is not a function . No entiendo el problema aquí, gracias de antemano.

esquema:-

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

llamada api:-

 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" }); }) }) }

aquí está el cartero: ingrese la descripción de la imagen aquí

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Prueba esto:

 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!