我在网上商店中创建商品订单,但是当我执行POST请求时
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)
})
}
此处为jsx代码
<div class="descCont"><button class="add" @click="addToCart">Add to Cart</button></div>
我弄错了:
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)
我检查了体内传入数据的正确性,一切 纠正那里。你能告诉我我错了吗? 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
})
})
})
在主文件中:
const ordersRoutes = require('./routes/orders')
答案 0 :(得分:1)
也许您发布的商品ID错误?在您的第一个陈述中,您的第一个if语句将返回404
错误。
请注意,product: this.id,
可能是undefined