Okay so this is exactly how it sounds but let me explain. Im creating a CRUD application with mern stack and im starting with the backend first. Its the first app im creating by myself so its pretty basic. It only has two models, the User model and the Product model. When i was creating the Product model, i added an image property and gave it a type of object. not even sure if that is correct. Im done with the authentication part so im starting with the 'create product' route. I know that handling image upload is different than handling other properties. so how would i handle the image upload when creating a product? I would post some code below for context.
my product model:
const mongoose = require('mongoose')
const ProductSchema = new mongoose.Schema({
name:{
type: String,
required: [true, 'please provide a product name'],
maxlength: 20,
minlength: 3
},
category: {
type: String,
required: [true, 'please provide a category'],
maxlength: 20,
minlength: 3
},
quantity: {
type: Number,
required: [true, 'please provide the quantity']
},
price: {
type: Number,
required: [true, 'please provide the price']
},
description: {
type: String,
required: [true, 'please provide the description'],
trim: true
},
image: {
type: Object,
default: {}
},
createdBy: {
type: mongoose.Types.ObjectId,
ref: 'User',
required: [true, 'Please provide the user'],
},
},
{ timestamps: true }
)
module.exports = mongoose.model('Product', ProductSchema)
my product controller:
const Product = require('../models/Product')
const getAllProducts = async (req, res) => {
res.send('get All products')
}
const createProduct = async (req, res) => {
res.send('create Product')
}
const getProduct = async (req, res) => {
res.send('get product')
}
const updateProduct = async (req, res) => {
res.send('update product')
}
const deleteProduct = async (req, res) => {
res.send('delete product')
}
module.exports = {
getAllProducts, createProduct, getProduct, updateProduct, deleteProduct
}