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

187
Vistas
Vue - How to send data array to Mongodb?
{
    "product": [
        {
            "product_ID": "test",
            "productDetail": "test",
            "income_ID": "6297343ec4cc7b1ca85521ba"
        },
        {
            "product_ID": "test",
            "productDetail": "test",
            "income_ID": "6297343ec4cc7b1ca85521ba"
        }
    ]
}

this my data array I used axios send data to express and mongodb but it's not working

and this my express code to save data in mongodb

quotationRoute.route('/incomeProduct').post((req, res, next) => {

    var newProduct = new Qot({
        product:product.req.body.product,
        
    })
    newProduct.save(err => {
        if (err) {
            return res.status(400).json({
                title: 'error',
                error: 'error'
            })
        }
        return res.status(200).json({
            title: 'signup success',
            id: newProduct._id

        })
    })
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

If your schema is the following:

{
  productDetail: "String",
  productQyt: "String",
  productPrice: "String",
  productUnit: "String",
  createdAt: {
    type: "Date",
    default: "Date.now",
  },
  income_ID: {
    type: "mongoose.Schema.Types.ObjectId",
    ref: "Business",
  },
  product_ID: "String",
};

And the data you are sending to your server is:

{
    "product": [
        {
            "product_ID": "test",
            "productDetail": "test",
            "income_ID": "6297343ec4cc7b1ca85521ba"
        },
        {
            "product_ID": "test",
            "productDetail": "test",
            "income_ID": "6297343ec4cc7b1ca85521ba"
        }
    ]
}

You have an array of objects so it looks like you want to save multiple quotations for that purpose you could use the insertMany methods of mongoose

here is an example of how your code needs to be:

quotationRoute.route('/incomeProduct').post(async (req, res, next) => {
  try {
    await Qot.insertMany(req.body.product);
    return res.status(200).json({
      title: 'signup success',
      //  id: newProduct._id as you save multiple Qot this will be unecessary
    });
  } catch (e) {
    return res.status(400).json({
      title: 'error',
      error: 'error',
    });
  }
});
over 4 years ago · Santiago Trujillo 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