繁体   English   中英

如何在猫鼬中添加通知?

[英]How to Add Notifications in mongoose?

我有这个用户架构

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;

我想在用户创建博客或添加评论时向所有人发送通知,我该如何实现? 我应该使用触发器吗?

这背后的策略

  • 您有多个用户。
  • 您有多个通知,可能针对单个用户、某些用户或所有用户。
  • 您需要存储中的通知“阅读”条目,以了解用户是否已阅读通知。

我想这可以通过使用 mongodb 更改流轻松实现。

您可以通过代码示例查看更多关于此处的信息。

mongodb 改变流的力量

基本上监听插入/更新操作类型并做出相应的反应。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM