【问题标题】:Mongoose: Allow users one review per journalMongoose:允许用户对每个期刊进行一次评论
【发布时间】:2021-10-23 15:03:54
【问题描述】:

我有三个架构:

const journalSchema = new mongoose.Schema({
  title: String,
  category: String,
  subcategory: String,
  review: [{type: mongoose.Schema.Types.ObjectId, ref: 'Review'}],
  link: String,
  description: String,
  subscribers: Number,
  posts: Number,
  image: {
    data: Buffer,
    contentType: String},
  date: Date
});


const userSchema = new mongoose.Schema ({
  username: String,
  nickname: String,
  password: String,
  journal: [{type: mongoose.Schema.Types.ObjectId, ref: 'Journal'}],
  googleId: String,
  age: {type: Date},
  gender: {type: String, enum: ["male", "female"]},
  admin: Boolean,
  role: String
});

const reviewSchema  = new mongoose.Schema({
  author: {type: mongoose.Schema.Types.String, ref: 'User'},
  content: String,
  date: Date,
  rating: {type: Number, min: 1.0, max: 5.0}
});


const Journal = mongoose.model("Journal", journalSchema);
const User = mongoose.model("User", userSchema);
const Review = mongoose.model("Review", reviewSchema);

现在任何用户都可以在同一期刊上发表任意数量的评论。我想做到这一点,以便用户每本期刊只能留下一条评论。

获取评论的发布路径:

app.post("/stats/review", function(req, res){
  if(req.isAuthenticated()){
    const userNickname = req.user.nickname;
    const userId = req.user.id;
    const userReview = req.body.journalReview;
    const userRating = req.body.rating;
    const journalId = req.body.journalId;
    Journal.findById({_id: journalId}, function(err, journal){

        Review.find({_id: {$in: journal.review}}, function(err, foundReview){
          foundReview.forEach(function(review){
            if(review.author == userNickname){
              console.log("Review from this user already exists");
            }

            else{
              var date = new Date();
              const review = new Review();
              review.author = userNickname;
              review.content = userReview;
              review.rating = userRating;
              review.date = date;
              review.save()
                .then((result) =>{
                  Journal.findOneAndUpdate(
                    {_id: journalId},
                    {$push: {
                      review: review
                    }},
                    {useFindAndModify: false},
                    function(err, success){
                      if(err){

                        console.log(err);
                      }
                      else{
                        res.redirect("back");
                      }
                    }
                  );


                })
                .catch((error) =>{
                  console.log(error);
                })
            }
          });

        })
    })






  }


  else{
    res.redirect("/login");
  }

});

是否可以使用 addToSet mongoose 方法来实现?无法从类似问题中找到合适的解决方案。

【问题讨论】:

    标签: node.js mongodb express mongoose ejs


    【解决方案1】:

    我认为你需要在这里做一些检查

    如果_id 已经存在于评论字段中,则返回一些错误消息

    【讨论】:

    • 我试图做一些检查,它可以检查用户是否正在为同一家商店写另一条评论,但对于新商店没有任何反应。编辑了问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-11
    • 1970-01-01
    • 2011-10-15
    • 2015-03-12
    • 1970-01-01
    • 2020-03-01
    相关资源
    最近更新 更多