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
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.