【发布时间】:2020-02-27 22:14:45
【问题描述】:
我有这个用户架构
const Schema = mongoose.Schema;
const bcrypt = require("bcryptjs");
const userSchema = new Schema(
{
email: {
type: String,
required: true,
index: {
unique: true
}
},
password: {
type: String,
required: true
},
name: {
type: String,
required: true
},
website: {
type: String
},
bio: {
type: String
}
},
{
timestamps: {
createdAt: "created_at",
updatedAt: "updated_at"
},
toJSON: { virtuals: true }
}
);
userSchema.virtual("blogs", {
ref: "Blog",
localField: "_id",
foreignField: "author"
});
userSchema.pre("save", function(next) {
const user = this;
if (!user.isModified("password")) return next();
bcrypt.genSalt(10, function(err, salt) {
if (err) return next(err);
bcrypt.hash(user.password, salt, function(err, hash) {
if (err) return next(err);
user.password = hash;
next();
});
});
});
userSchema.methods.comparePassword = function(password, next) {
bcrypt.compare(password, this.password, function(err, isMatch) {
if (err) return next(err);
next(null, isMatch);
});
};
const User = mongoose.model("User", userSchema);
module.exports = User;
我想在用户创建博客或添加评论时向所有人发送通知,我该如何实现?我应该使用触发器吗?
这背后的策略
- 您有多个用户。
- 您有多个通知,可能针对单个用户、某些用户或所有用户。
- 您需要存储中的通知“已读”条目,以了解用户是否已阅读通知。
【问题讨论】:
标签: javascript node.js mongodb mongoose