Reputation: 323
I create orders for goods in the online store but when I do POST request
addToCart: function () {
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)
})
}
Here in jsx code
<div class="descCont"><button class="add" @click="addToCart">Add to Cart</button></div>
I get err :
POST localhost:8081/orders 404 (Not Found)
Error: Request failed with status code 404
at createError (createError.js?16d0:16)
at settle (settle.js?db52:18)
at XMLHttpRequest.handleLoad (xhr.js?ec6c:77)
I checked the correctness of the incoming data in the body and everything is correct there. Can you tell me what I was wrong about? Server code on Node.js(orders.js)
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
})
})
})
In main file:
const ordersRoutes = require('./routes/orders')
Upvotes: 0
Views: 841
Reputation: 71
Maybe you post a product with a wrong Id? With your first statment your first if statement will return a 404
error.
Please be aware that product: this.id,
might be undefined
Upvotes: 1