Answer the question
In order to leave comments, you need to log in
Why can't I add an order to the database?
I am creating orders for goods in an online store, but when I make a POST request
addToCart: function (productId) {
axios.post('http://localhost:8081/orders/',{
quantity: '1',
product: this.id,
user: this.$store.getters.getUser._id
},{
headers: {
Authorization:"Bearer " + this.$store.getters.getToken
}
})
.then(res => {
console.log('sucsecc')
})
.catch(err => {
console.log(err)
})
}
router.post('/',checkAuth,upload.single('productImge'), (req, res, next) => {
Product.findById(req.body.productId)
.then(product => {
if (!product){
return res.status(404).json({
message: 'Product not found'
})
}
const order = new Order({
_id: mongoose.Types.ObjectId(),
quantity: req.body.quantity,
product: req.body.productId,
user: req.body.user
})
return order.save()
})
.then(result => {
res.status(201).json({
message: 'Order stored',
createdOrder: result
})
})
.catch(err => {
console.log(err)
res.status(500).json({
error: err
})
})
})
Answer the question
In order to leave comments, you need to log in
router hangs on path `/orders`?
what kind of middleware is `upload.single('productImge')`?
Is there really a product with this ID?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question