获取NodeJS删除路由不起作用

时间:2018-12-28 08:25:57

标签: javascript node.js routes

我现在成功地完成了注册部分,我试图通过id删除注册用户。这就是我的做法

用户控制器

const mongoose = require ('mongoose');
const User = mongoose.model('User');

module.exports.register = (req, res, next) =>{  

    var user = new User();
    user.fullName = req.body.fullName;
    user.email = req.body.email;
    user.password = req.body.password;
    user.phoneNumber = req.body.phoneNumber;
    user.save((err, doc) =>{
        if(!err)
            res.send(doc);
        else{
            if (err.code == 11000)
                    res.status(422).send(["Entered duplicate email address. Please check"]);
            else
            return next(err);
            }
    });
}

在我的索引路由器

const express = require ('express');
const router = express.Router();
const ctrlUser = require ('../controllers/user.controller.js');

// routing functions 

router.post('/register' , ctrlUser.register);

router.delete('/:userId' , (req, res, next) =>{

    User.remove({_id: req.params.userId})
    .exec()
    .then(result => {
        res.status(200).send(["Deleted"]);
    })
    .catch(err =>{
        if (err.code == 500)
                    res.status(500).send(["Didn't get deleted"]);
            else
            return next(err);
    });
});
module.exports = router; 

在邮递员中进行测试时,我没有得到任何回应。我在哪里弄错了?我错过了什么吗?

编辑:- 包括user.model

const mongoose = require ('mongoose');
const bcrypt = require('bcryptjs');

//define user schema 
var userSchema = new mongoose.Schema({
    fullName : {
                type: String,
                required: "Full name can't be empty"
                },
    email :     {
                type: String,
                required: "Email can't be empty",
                unique: true
                },
    password :  {
                type: String,
                required: "Password can't be empty",
                minlength: [6 ,"Password must be atleast 6 character long"]
                },  
    phoneNumber :   {
                type: String,
                required: "Reqired for further contact.Can't be empty"
                },
    saltSecret: String //this is user for encryption and decryption of password
});
mongoose.model('User', userSchema);

0 个答案:

没有答案