0

I am trying to get my users roles to update in the comments model. after hours os googling I am turning to StackOverflow to help me.I have an admin tool that where I can edit each users role, but it does not effect the users role data in the comments. Posted below are the user model, and the comments model.

    var mongoose = require("mongoose");
    var commentSchema = new mongoose.Schema({
    text: String,
    author: {
        id: {
            type: mongoose.Schema.Types.ObjectId,
            ref: "User"
        },
        username: String,
        isAdmin: { type: Boolean, default: false },
        isOwner: { type: Boolean, default: false },
        isCamper: { type: Boolean, default: false },
    },
    time: String

});

module.exports = mongoose.model("Comment", commentSchema);

user model

const mongoose = require("mongoose"),
passportLocalMongoose = require("passport-local-mongoose"),
UserSchema = new mongoose.Schema({
    email: { type: String, unique: true, required: true },
    username: { type: String, unique: true, required: true },
    password: String,
    isAdmin: { type: Boolean, default: false },
    isOwner: { type: Boolean, default: false },
    isCamper: { type: Boolean, default: false },
    resetPasswordToken: String,
    resetPasswordExpires: Date,
    comments: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: "Comment"
    }]
});
UserSchema.plugin(passportLocalMongoose);

module.exports = mongoose.model("User", UserSchema);
  • Can i see how you query your comment? – dnp1204 Jul 23 '18 at 20:52
  • ` Campground.findById(req.params.id).populate('comments').exec` `<%campground.comments.forEach(function(comment){%>` – samhughlett Jul 23 '18 at 20:54
  • What is your Campground? Is it user model? In your commentSchema, I would suggest you should change to author: { type: mongoose.Schema.Types.ObjectId, ref: "User" }. and then to find an comment, you can do Comment.findById(id).populate('author') – dnp1204 Jul 23 '18 at 20:59
  • Or you can do deep populate by Campground.findById(req.params.id).populate({ path: 'Comment', populate: 'author' }) – dnp1204 Jul 23 '18 at 21:01
  • Possible duplicate of [MongoDB: How do I update a single subelement in an array, referenced by the index within the array?](https://stackoverflow.com/questions/11372065/mongodb-how-do-i-update-a-single-subelement-in-an-array-referenced-by-the-inde) – Ashh Jul 24 '18 at 04:27

0 Answers0