sei-ec-remote / project-4-issues

Open an issue to receive help on project 4
0 stars 0 forks source link

Trying to populate owner information into comments #113

Closed bostonbachexchange closed 2 years ago

bostonbachexchange commented 2 years ago

What stack are you using?

MERN(mongoose + react)

What's the problem you're trying to solve?

I am trying to populate the owner information into the comments(comment is subdocuments model of Message) . This would hopefully allow me to pull in user information like user name of comment creator and other things. I manage to assign the owner Id to the comment. The owner information is already populating in the message document, but not within the comment subdoc.

Post any code you think might be relevant (one fenced block per file)

// SHOW
// GET /messageboard/5a7db6c74d55bc51bdf39793
router.get('/messageboard/:id', (req, res, next) => {
    // req.params.id will be set based on the `:id` in the route
    MessageBoard.findById(req.params.id)
    .populate('owner')
        .then(handle404)
        // if `findById` is succesful, respond with 200 and "example" JSON
        // .then((message) => {
        //  if (message.comments) {
        //      console.log('message.comments', message.comments)
        //      // message.comments.find()
        //      // .populate('owner')
        //  }
        // })
        .then((message) => res.status(200).json({ message: message.toObject() }))
        // if an error occurs, pass it to the handler
        .catch(next)
})

If you see an error message, post it here. If you don't, what unexpected behavior are you seeing?

cannot read properties of undefined

What is your best guess as to the source of the problem?

Comments are show on the message page, not their own show page. I am guess they need to be populated from the message_routes.

What things have you already tried to solve the problem?

        // .then((message) => {
        //  if (message.comments) {
        //      console.log('message.comments', message.comments)
        //      // message.comments.find()
        //      // .populate('owner')
        //  }

Paste a link to your repository here https://github.com/bostonbachexchange/project_4_API.git

timmshinbone commented 2 years ago

Try adapting your populate message to include multiple fields, like this:

.populate('owner', 'comments.owner')

Another more technical population method looks like this:

.populate({
    path:     'comments',           
    populate: { path:  'owner',
            model: 'users' }
  })
timmshinbone commented 2 years ago

If you share what the models look like it'll be easier for me to say how this should be written out. Sharing the data you're currently receiving from the API will help even further

bostonbachexchange commented 2 years ago

MODELS:

const mongoose = require('mongoose')
const commentSchema = require('./comment')
// const user = require('./user')
const { Schema, model } = mongoose

const messageBoardSchema = new Schema(
    {
        name: {
            type: String,
        },
        title: {
            type: String,
        },
        content: {
            type: String,
            required: true,
        },
        date: {
            type: Date,
            default: Date.now,
        },
        comments: [commentSchema],
        owner: {
            type: Schema.Types.ObjectId,
            ref: 'User',
            required: true,
        },
    },
    {
        timestamps: true,
    }
)

module.exports = model('MessageBoard', messageBoardSchema)
const mongoose = require('mongoose')
// const { Schema, model } = mongoose

const commentSchema = new mongoose.Schema(
    {
        content: {
            type: String,
            required: true,
        },
        date: {
            type: Date,
            default: Date.now,
        },
        owner: {
            type: mongoose.Schema.Types.ObjectId,
            ref: 'User',
            // required: true
        },
    },
    {
        timestamps: true,
    }
)

module.exports = commentSchema
bostonbachexchange commented 2 years ago

oh man.... my indentation is not like that i promise

timmshinbone commented 2 years ago

Don't worry I fixed the formatting. What's the data look like when you get a response from the route?

bostonbachexchange commented 2 years ago

Screen Shot 2022-08-24 at 10 57 30 AM

timmshinbone commented 2 years ago

Ok, did you try adjusting your populate message? Should look a little something like this:

MessageBoard.findById(req.params.id)
    .populate('owner', 'comments.owner')
bostonbachexchange commented 2 years ago
MessageBoard.findById(req.params.id)
.populate('owner', 'comments.owner')
    .then(handle404)

ok, just tried this one, still no dice

timmshinbone commented 2 years ago

any change in response data?

bostonbachexchange commented 2 years ago

Screen Shot 2022-08-24 at 11 06 01 AM

not that i can tell

timmshinbone commented 2 years ago

ok, let's check it out in a breakout room

bostonbachexchange commented 2 years ago

Thanks, Tim! Adding comments.owner in the array works

MessageBoard.findById(req.params.id) .populate(['owner', 'comments.owner']) .then(handle404)