I saw a very similar problem like this on stackoverflow but the answer didn’t solve my problem. I am receiving an error apparently. I have created a post request in thunderclient in VS Code.
The first one runs fine but then the next ones
enter image description here
have issue
(https://i.sstatic.net/jy2LVl0F.png)
and show this error:
enter image description here
This is my code – I tried changing the unique:false in the code and figuring out what to do but nothing works. (https://i.sstatic.net/XI0M1aCc.png)
const port = 4000;
const express = require('express');
const app = express();
const mongoose = require('mongoose');
const jwt = require('jsonwebtoken');
const multer = require('multer');
const path = require('path');
const cors = require('cors');
const { error } = require('console');
const { type } = require('os');
const { json } = require('express');
// Connect cors to port
app.use(express.json());
app.use(cors());
// Initialise database connection with mongoDB
mongoose.connect("mongodb+srv://aryankap:[email protected]/e-commerce");
// API creation
app.get("/", (req,res)=>{
res.send("Express App is Running")
})
// Image Storage Engine
const storage = multer.diskStorage({
destination: './upload/images',
filename:(req,file,cb)=>{
return cb(null, `${file.fieldname}_${Date.now()}${path.extname(file.originalname)}`)
}
})
const upload = multer({storage:storage})
// Creating Upload Endpoint for images
app.use('/images',express.static('upload/images'))
app.post("/upload",upload.single('product'),(req,res)=>{
res.json({
success:1,
image_url:`http://localhost:${port}/images/${req.file.filename}`
})
})
// Schema for Creating Products
const Product = mongoose.model("Product",{
id:{
type:Number,
required:true,
},
name:{
type:String,
required:true,
},
image:{
type:String,
required:true,
},
category:{
type:String,
required:true,
},
new_price:{
type:Number,
required:true,
},
old_price:{
type:Number,
required:true,
},
date:{
type:Date,
default:Date.now,
},
available:{
type:Boolean,
default:true,
},
})
app.post('/addproduct',async (req,res)=>{
let products = await Product.find({});
let id;
if(products.length>0)
{
let last_product_array = products.slice(-1);
let last_product = last_product_array[0];
id = last_product.id+1;
}
else{
id=1;
}
const product = new Product({
id:id,
name:req.body.name,
image:req.body.image,
category:req.body.category,
new_price:req.body.new_price,
old_price:req.body.old_price,
});
console.log(product);
await product.save();
console.log("Product Added Successfully");
res.json({
success:true,
name:req.body.name,
})
})
// Creating API for deleting products
app.post('/removeproduct',async (req,res)=>{
await Product.findOneAndDelete({id:req.body.id});
console.log("Removed");
res.json({
success:true,
name:req.body.name,
})
})
// Creating API for getting all products
app.get('/allproducts',async (req,res)=>{
let products = await Product.find({});
console.log("All Products Fetched");
res.send(products);
})
// Schema Creation for User model
const Users = mongoose.model('Users', {
name:{
type:String,
},
email:{
type:String,
unique:true,
},
password:{
type:String,
},
cartData:{
type:Object,
},
date:{
type:Date,
default:Date.now,
}
})
// creating endpoint for user registration
app.post('/signup',async (req,res)=>{
let check = await Users.findOne({email:req.body.email});
if (check) {
return res.status(400).json({success:false,errors:"Account already exists with this email"})
}
let cart = {};
for (let i = 0; i < 300; i++) {
cart[i]=0;
}
const user = new Users({
name:req.body.username,
emai:req.body.email,
password:req.body.password,
cartData:cart,
})
await user.save();
const data = {
user:{
id:user.id
}
}
const token = jwt.sign(data,'secret_ecom');
res.json({success:true,token})
})
app.listen(port,(error)=>{
if (!error) {
console.log("Server is running on port: "+port)
}
else
{
console.log("Error: "+error)
}
})
These are the thunderclient requests:
Signup request 1
Upload Request
Add Product request
All Products request(This one is using GET all others are using POST)
And lastly the remove product request
I couldn’t add screenshots for all the requests all stackoverflow won’t let me due to low reputation
4