AJAX DELETE遇到问题

I'm not sure what I'm doing wrong here? The GET works fine with the same info, but the DELETE keeps giving me a 500 Internal server error, and my server logs say, "StoredProduct.delete is not a function"

For the sake of this post, I'm including the GET route and GET JS below, just to show those are working, so I think my routes are set up correctly?

These are my routes

router.get('/:productID', (req, res, next) => {
  StoredProduct
    .findOne({
      _id: req.params.productID
    })
    .then(product => {
      res.status(201).json(product);
    });
});

router.delete('/:productID', (req, res) => {
  StoredProduct
    .delete(req.params.productID);
  res.status(204).end()
    .catch(err => {
      console.log(err);
      res.status(500).json({
        error: err
      });
    });
});

And this is my JS

  $.ajax({
    type: 'GET',
    url: '/products/' + productID,
    success: function(product) {
      $editProdName.append(`${product.name}`);
      $addPrice1.val(product.prices[0].price);
      $addPrice2.val(product.prices[1].price);
      $addPrice3.val(product.prices[2].price);
      $selectedUnit.val(product.size);
    }
  });

  $('#deleteme').on('click', function(e) {
    e.preventDefault();
    console.log(productID);
    $.ajax({
      type: 'DELETE',
      url: '/products/' + productID,
      success: function(){
        console.log('yippee');
      }
    });
  });
查看全部
weixin_33738982
?Briella
2018/11/29 15:13
  • node.js
  • ajax
  • 点赞
  • 收藏
  • 回答
    私信

2个回复