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

144
Vistas
How to save array of Strings in Node Mongodb

I Have an array of String which I want to save in Mongodb in Node/express, it is saving Empty array in Mongodb

const multiImagesSchema = mongoose.Schema({
    names:Array
})

import ImgArray from '../../Model/multiImages.js'

let imgNames = req.files.map(imgName=>{ 
            return imgName.originalname  
        })

      // console,log(imgNames)
       //result in console:   ['Apple,png', 'Grapes.png','Ornage.png','Banana.png'] 

        const imageArray = new ImgArray(imgNames)
        await imageArray.save()
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You have to pass the object with properties defined in schema Model to the creation function (in your case the object with names property). You are passing the imgNames array. Change you code like this:

await ImgArray.create({ names: imgNames });
about 4 years ago · Juan Pablo Isaza Denunciar

0

your model need to have a field that accepts array like this:

const ImgArraySchema = mongoose.Schema({
names: [{
type: String,
required: true
}]
})

then to save an array, do it like this:

const imageArray = new ImgArray({names: imgNames})
const imgarray = await imageArray.save()
.catch((err) => (res.status(500).send({err: err.message})))
if (imgarray) {
return res.status(200).send({success: true, imgarray})
}
return res.status(400).send({success: false, error: ""})
about 4 years ago · Juan Pablo Isaza Denunciar
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