I am learning Next.js currently, and I have a question.
I have this data as a req.body:
{
"name": "Name Here",
"phone": [
{
"phonetype": "Work",
"phonenumber": "PhoneNumber1"
},
{
"phonetype": "Home",
"phonenumber": "PhoneNumber2"
}
],
"city": "CityHere"
}
And I am trying to create a new Customer in the database. Below is my POST:
case 'POST':
try{
const customer = req.body;
const newCustomer = new Customer({
name: customer.name,
//phone: HOW TO I HANDLE THIS ARRAY?
city: customer.city,
});
const createdCustomer = await Customer.create(newCustomer);
res.status(201).json({success: true, data: createdCustomer});
} catch (error) {
res.status(400).json({success: false, data: "Error Creating Customer"});
}
break;
How do I go about submitting it to the database (using Mongoose)?
Schema below:
const CustomerSchema = new Schema({
name: {
type: String,
required: true,
trim: true,
},
phone: {
phonetype: {
type: String,
},
phonenumber: {
type: String,
},
},
city: {
type: String,
required: true,
trim: true,
}
})