Closed irishjack490 closed 9 months ago
Share your model, I need to see the pieces that this thing is made up of.
Why are you using a try catch block instead of the then...catch style promise chain provided by my examples? Not judging the decision to do so, just wondering why the decision was made.
Here is the model for the order:
const mongoose = require('mongoose')
const orderSchema = new mongoose.Schema(
{
donuts : {
type: mongoose.Schema.Types.ObjectId,
ref: 'Donut',
required: true
},
donutQuantity: {
type: Number,
default: 0
},
coffee: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Coffee',
required: true
},
coffeeQuantity: {
type: Number,
default: 0
},
totalPrice: {
type: Number,
required: true,
},
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
},
createdAt: {
type: String,
default: Date.now
}
}
)
module.exports = mongoose.model('Order', orderSchema)
Just in case, here is how I did the seeding for the order:
const Coffee = require('./coffee')
const User = require('./user')
const db = require('../../config/db')
const userId = '65cb7fe3825b95a713192701' //testing user
const donutId = '65ca6d39931f775ef6a69741' //testing donut
const coffeeId = '65ca7db77b22de6ae35a0a8a' //testing coffee
const startOrders = [
{ donuts: donutId,
donutQuantity: 1,
coffee: coffeeId,
coffeeQuantity: 1,
totalPrice: 3.00,
user: userId,
createdAt: new Date().toISOString()
}
]
mongoose.connect(db, {useNewUrlParser: true})
.then(() => {
Order.deleteMany({ owner: null })
.then(deletedOrders => {
console.log('deleted orders in seed script: ', deletedOrders)
Order.create(startOrders)
.then(newOrders => {
console.log('new orders added to db: \n', newOrders)
// VERY IMPORTANT
mongoose.connection.close()
})
.catch(error => {
console.log('an error has occurred: \n', error)
// VERY IMPORTANT
mongoose.connection.close()
})
})
.catch(error => {
console.log('an error has occurred: \n', error)
// VERY IMPORTANT
mongoose.connection.close()
})
})
.catch(error => {
console.log('an error has occurred: \n', error)
// VERY IMPORTANT
mongoose.connection.close()
})
The reason for trying a catch block, that is a good question, I was trying to do something I remember for practice but that was not the best idea. Thanks so much for the hlep
So, are you console logging the request body anywhere? If not, you should add that at the top. Also, in your model, the field 'donuts' is a singular id, but named in the plural. In the data you're sending, you're sending it as 'donut' which would be a more syntactically accurate name for that field, considering it's set up to be a single id reference.
Thanks so much for the help that worked! I used req.body.order.user = req.user.id, I pretty much changed the route to look like your code from pets app, and changed the donut to singular. It worked I was able to create order in Postman.
What stack are you using?
(ex: MERN(mongoose + react), DR(django + react), PEN, etc.)
MERN
What's the problem you're trying to solve?
Trying to test route for a user to create an order on postman, I get error: "Failed to create order"
Post any code you think might be relevant (one fenced block per file)
Here is the code for the route
Here is what I am trying to put in on postman:
If you see an error message, post it here. If you don't, what unexpected behavior are you seeing?
These are the errors I am seeing in the console: Error creating order Error: Order validation failed: donuts: Path
donuts
is required., coffee: Pathcoffee
is required., totalPrice: PathtotalPrice
is required. at ValidationError.inspect (/home/irishjack/sei/projects/Project4/DonutShopApp/DonutsAPI/node_modules/mongoose/lib/error/validation.js:50:26) at formatValue (node:internal/util/inspect:805:19) at inspect (node:internal/util/inspect:364:10) at formatWithOptionsInternal (node:internal/util/inspect:2279:40) at formatWithOptions (node:internal/util/inspect:2141:10) at console.value (node:internal/console/constructor:352:14) at console.warn (node:internal/console/constructor:385:61) at /home/irishjack/sei/projects/Project4/DonutShopApp/DonutsAPI/app/routes/order_routes.js:53:11 at process.processTicksAndRejections (node:internal/process/task_queues:95:5) { errors: { donuts: ValidatorError: Pathdonuts
is required. at validate (/home/irishjack/sei/projects/Project4/DonutShopApp/DonutsAPI/node_modules/mongoose/lib/schematype.js:1365:13) at SchemaType.doValidate (/home/irishjack/sei/projects/Project4/DonutShopApp/DonutsAPI/node_modules/mongoose/lib/schematype.js:1349:7) at /home/irishjack/sei/projects/Project4/DonutShopApp/DonutsAPI/node_modules/mongoose/lib/document.js:2955:18 at process.processTicksAndRejections (node:internal/process/task_queues:77:11) { properties: [Object], kind: 'required', path: 'donuts', value: undefined, reason: undefined,}, _message: 'Order validation failed' }
What is your best guess as to the source of the problem?
Not 100% most likely the route
What things have you already tried to solve the problem?
I have tried to read over mongoose documentation but I need help, thank you
Paste a link to your repository here https://github.com/irishjack490/Project4-DonutShop-MERN-Stack-App