I have a POST request in my Nextjs app, file path is app/api/gigs/new. The request is supposed to check for data in the request body, take that data and add it to the mongo db schema, then save it to the database. When I try sending the request via thunderclient(a tool like postman), I get the following error:
“Data has not been saved successfully: ValidationError: GigTitle: Path GigTitle
is required., GigDesc: Path GigDesc
is required., BasicPrice: Path BasicPrice
is required., StandardPrice: Path StandardPrice
is required., BasicFeatures: Path BasicFeatures
is required., StandardFeatures: Path StandardFeatures
is required.“.
The kind of payload that I am sending through thunder client is like this:
{
“gigtitle”: “Gigger”,
“gigdesc”: “Desc”,
“basicprice”: “20”,
“basicfeatures”: “Unlimited revisions”,
“stdprice”: “50”,
“stdfeatures”: “three revisions”
}
Here is my route.js file through which I am trying to send the request:
import { connectToDB } from "../../../../lib/mongo.js";
import Gig from "../../../../lib/models/Gigs.model.js";
export const POST = async (req) => {
try {
await connectToDB();
const {
gigtitle,
gigdesc,
basicprice,
basicfeatures,
stdprice,
stdfeatures,
} = req.body;
const gig = await new Gig({
GigTitle: gigtitle,
GigDesc: gigdesc,
// GigTags: tags,
BasicPrice: basicprice,
BasicFeatures: basicfeatures,
StandardPrice: stdprice,
StandardFeatures: stdfeatures,
});
await gig.save();
return new Response("Data saved successfully", { status: 200 });
} catch (error) {
console.log(error);
return new Response(`Data has not been saved successfully: ${error}`, {
status: 500,
});
}
};
Here is the mongo db schema:
import mongoose from "mongoose";
const GigSchema = new mongoose.Schema(
{
GigTitle: {
type: String,
required: true,
},
GigDesc: {
type: String,
required: true,
},
GigTags: {
type: Array,
},
BasicPrice: {
type: Number,
required: true,
},
StandardPrice: {
type: Number,
required: true,
},
BasicFeatures: {
type: String,
required: true
},
StandardFeatures: {
type: String,
required: true
},
Sales: {
type: Number,
default: 1
}
},
{ timestamps: true }
);
const Gig = mongoose.models.Gig || mongoose.model("Gig", GigSchema);
export default Gig;
What am i doing wrong here? Please someone advice.