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

145
Vistas
What is the best way to send both Image and JSON to my express server?

Ok, so I got a mongodb database, and I am using mongoose.

I have the following model for my product.

const productSchema = new Schema({
  name: { type: String, required: true},
  description: { type: String, required: true },
  price: { type: Number, required: true },
  image: { type: String, required: true },
  
  usage: { type: String, required: true },
  species: [{
    specie: { type: String, required: true }
  }],
  ingredients: [{
    name: { type: String, required: true },
    quantity: { type: String, required: true },
  }],
});

What I am trying to do is send both an image and JSON in a SINGULAR http request. I found out that this is not really possible. So what is the best to achieve this? Send two different HTTP requests, one with the json/text data, and one with the file? This would mean that I need to edit my model and make the fields unrequired, and it would be a bit rough to debug in case something goes wrong with one of the requests.

  const newProduct = {
        name: name,
        description: description,
        price: price,
        usage: usage,
        ingredients: itemList,
        species: speciesList,
        image: image,
      };


     const [itemList, setItemList] = useState([
    { name: "", quantity: "" },
  ]);
  const [speciesList, setSpeciesList] = useState([
    { specie: "" },
  ]);

This is what I am trying to send

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

It's simple. you can send the data to the express server with multipart/form-data structure and then in the server should parse received data and map the values as defined model and finally store into mongo db.

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