原谅我,因为我对Mongoose和MEAN一般都是新手。
我很好奇,如果像这样的if语句可以防止用户将自己推入数组两次。
现在我正在使用ng-hide隐藏“出席”按钮,但我想了解一种在猫鼬中完全阻止它的方法。
export function attending(req: express.Request, res: express.Response, next: Function){
if(req.params.id.attending.indexOf(req['payload']._id) !== -1) {
Event.update({_id: req.params.id}, {$push: {'attending': req['payload']._id }, $inc: {numGuests: -1}}, (err)=> {
if (err) return next (err)
User.update({user}, {$push: {'attending': event}}, (err)=> {
if(err) return next (err);
res.json({message: "You're In!"});
})
});
}
}
这是模型
let eventSchema = new mongoose.Schema({
title: {type: String, required: true},
name:{type: String, required: true},
eventAddress: { type: String, required: true},
dateCreated: {type: Number},
eventCreator: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true },
attending: [{ type: mongoose.Schema.Types.ObjectId, ref: 'User'}]
});
感谢您的任何建议!