【问题标题】:How do I delete a mongodb collection from a restapi [duplicate]如何从restapi中删除mongodb集合[重复]
【发布时间】:2018-11-21 07:08:01
【问题描述】:

我正在尝试从我的 mongo 收藏中删除一个用户,但无法弄清楚我如何分享我的注册请求和床位尝试。

api.post('/register', (req, res) => {
  Account.register(new Account({ username: req.body.email,
    phoneNumber: req.body.phoneNumber, nyuId: req.body.nyuId}), req.body.password, function(err, account) {
    if (err) {
      return res.json({
                        "status": false,
                        "code" : 500,
                        "message": 'An error occurred: ' + err
                      });
    }
    passport.authenticate(
      'local', {
        session: false
    })(req, res, () => {

      });
      res.status(200).send('Welcome to nyu created new account');+      });
    });
  });

在这里需要帮助。

api.delete(':id', (req, res) => {
  const id = req.params.id;
    Account.findByIdAndRemove({_id: req.body.id }, (err) => {
      if (!err) {
             res.sendStatus(404);
      }
      else {
            res.sendStatus(200);
      }
  });
});

【问题讨论】:

    标签: node.js mongodb rest


    【解决方案1】:
    Account.findByIdAndRemove(id, function(err){
    
        if(err) {
            // handle the error
        } else {
            // your logic
        }
    
    })
    

    【讨论】:

    • 我已经更新了我的请求,您能否更正我的删除请求。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-05
    • 1970-01-01
    • 1970-01-01
    • 2015-09-06
    • 2017-02-21
    • 1970-01-01
    • 2017-04-15
    相关资源
    最近更新 更多