O
O
Oleksandr Tatarinov2018-07-07 00:03:15
JavaScript
Oleksandr Tatarinov, 2018-07-07 00:03:15

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)
      })
    }

i get error 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? Server code in Node.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
            })
        })
})

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Kiryushka Tsisar, 2018-07-07
@carlcox

Maybe you still need to set the path correctly ?

S
slava_brezovsky, 2018-07-07
@slava_brezovsky

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 question

Ask a Question

731 491 924 answers to any question