so this is my code, whenever I gonna insert data into my database it returns error 400 I don’t know the reason, I did other crud operations and it was fine.
import React from 'react';
import { Formik, Form, Field} from 'formik';
import axios from 'axios';
const AddProduct = () => {
const handleFileChange = (event, setFieldValue) => {
const file = event.currentTarget.files[0];
const reader = new FileReader();
reader.onloadend = () => {
const blob = new Blob([reader.result], {
type: file.type
});
setFieldValue('pic', blob);
};
reader.readAsArrayBuffer(file);
};
return (
<Formik
initialValues={{
name: '',
desc: '', // You can use a date picker library for this field
pic: '',
price: '',
}}
onSubmit={(values) => {
const formData = new FormData();
formData.append('name', values.name);
formData.append('category', values.category);
formData.append('desc', values.desc);
formData.append('price', parseFloat(values.price).toFixed(2));
formData.append('pic', values.pic);
axios.post('http://localhost:3002/products/', formData)
.then(response => {
console.log(response.data);
})
}}
>
{({ isSubmitting, setFieldValue }) => (
<Form className='addproduct-form-container'>
<Field type="text" name="name" placeholder="عنوان محصول" className='addproduct-product-name'/>
<Field type="text" name="desc" placeholder="توضیحات" className='addproduct-product-desc'/>
<input type="file" name="pic" placeholder="تصویر محصول" className='addproduct-product-pic' onChange={ (event) => handleFileChange(event, setFieldValue) }/>
<Field type="number" name="price" placeholder="قیمت محصول (تومان)" className='addproduct-product-price' />
<button type="submit" disabled={isSubmitting} className='addproduct-product-submit'>انجام</button>
</Form>
)}
</Formik>
)
}
export default AddProduct
when I log the form data it returns values and values are right but when I submit it returns error 400
it seems that the reason is notNull violation but as I said I logged the values before sending them.
routes/products.js:
const express = require('express');
const router = express.Router();
const Products = require('../models/Product');
//Create a Product
router.post('/', async (req, res) => {
try {
const product = await Products.create(req.body);
res.status(201).json(product);
} catch (error) {
res.status(400).json({ error: error.message });
}
});
// Read All Products
router.get('/', async (req, res) => {
try {
const products = await Products.findAll();
const imageBlobs = products.map(image => ({
id: image.id,
title: image.name,
description: image.description,
category: image.category,
price: image.price,
image: image.image.toString('base64')
}));
res.status(200).json(imageBlobs);
} catch (error) {
res.status(400).json({ error: error.message });
}
});
router.get('/:id', async (req, res) => {
try {
const product = await Products.findByPk(req.params.id);
if (product) {
res.status(200).json(product);
} else {
res.status(404).json({ error: 'Product not found' });
}
} catch (error) {
res.status(400).json( { error: error.message });
}
});
// Delete a Product By Id
router.delete('/:id', async (req, res) => {
try {
const product = await Products.findByPk(req.params.id);
if (product) {
await product.destroy();
res.status(204).send();
} else {
res.status(404).json( {error: 'Product Not Found.'});
}
} catch (error) {
res.status(400).json({ error: error.message });
}
});
module.exports = router;
11