Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

142
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda