Closed bostonbachexchange closed 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' }
})
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
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
oh man.... my indentation is not like that i promise
Don't worry I fixed the formatting. What's the data look like when you get a response from the route?
Ok, did you try adjusting your populate message? Should look a little something like this:
MessageBoard.findById(req.params.id)
.populate('owner', 'comments.owner')
MessageBoard.findById(req.params.id)
.populate('owner', 'comments.owner')
.then(handle404)
ok, just tried this one, still no dice
any change in response data?
not that i can tell
ok, let's check it out in a breakout room
Thanks, Tim! Adding comments.owner in the array works
MessageBoard.findById(req.params.id) .populate(['owner', 'comments.owner']) .then(handle404)
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)
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?
Paste a link to your repository here https://github.com/bostonbachexchange/project_4_API.git