我在用户和员工之间建立了一对一的关系。如何在删除与该用户相关的员工时删除该用户,反之亦然?
这是我的用户模型:
const mongoose = require('mongoose');
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
const UserSchema = new mongoose.Schema({
email: {
type: String,
required: true,
},
password: {
type: String,
required: true,
},
role: {
type: String,
enum: ['admin', 'user'],
default: 'user',
},
createdAt: {
type: Date,
default: Date.now,
},
});
// Encrypt password using bcrypt
UserSchema.pre('save', async function (next) {
if (!this.isModified('password')) {
next();
}
const salt = await bcrypt.genSalt(10);
this.password = await bcrypt.hash(this.password, salt);
});
// Sign JWT and return
UserSchema.methods.getSignedJwtToken = function () {
return jwt.sign({ id: this._id }, process.env.JWT_SECRET, {
expiresIn: process.env.JWT_EXPIRE,
});
};
// Match user entered password to hashed password in database
UserSchema.methods.matchPassword = async function (enteredPassword) {
return await bcrypt.compare(enteredPassword, this.password);
};
// Cascade delete courses when a bootcamp is deleted
UserSchema.pre('remove', async function (next) {
console.log(`Employee being removed from user ${this._id}`);
await this.model('Employee').deleteMany({ employee: this._id });
next();
});
const User = mongoose.model('User', UserSchema);
module.exports = User;
这是我的员工模型:
const mongoose = require('mongoose');
const employeeSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
lastName: {
type: String,
required: true
},
type: {
type: String,
enum: ['employee', 'manager'],
default: 'employee'
},
createdAt: {
type: Date,
default: Date.now
},
user: {
type: mongoose.Schema.ObjectId,
ref: 'User',
required: true
}
});
const Employee = mongoose.model('Employee', employeeSchema);
module.exports = Employee;
我尝试了以下操作:
// Cascade delete courses when a bootcamp is deleted
UserSchema.pre('remove', async function (next) {
console.log(`Employee being removed from user ${this._id}`);
await this.model('Employee').deleteMany({ employee: this._id });
next();
});
这是我的控制器中的deleteUser方法:
exports.deleteUser = asyncHandler(async (req, res, next) => {
// Ensure the request has a user object populated by the protect middleware
if (!req.user) {
return next(new ErrorResponse('Not authorized to delete this user', 401));
}
const user = await User.findById(req.params.id);
if (!user) {
return next(
new ErrorResponse(`User not found with id of ${req.params.id}`, 404)
);
}
user.deleteOne();
res.status(200).json({
success: true,
data: {},
});
});
但是我删除了一个员工,用户没有被删除。我错过了什么关于关系吗?
2条答案
按热度按时间1szpjjfi1#
对于User schema使用deleteOne操作的pre钩子
对于Employee schema使用deleteOne操作的pre钩子
gkn4icbw2#
你可能在其他地方遇到了问题,你的代码应该工作得很好,这是一个单文件工作示例,当它运行时,它会打印用户的id以便删除,只需点击带有id的端点
http://localhost:3000/delete/_id_here