I'm not sure how to render an object from an array into react.
My MongoDB schema is
const postSchema = new mongoose.Schema({
userID: { type: String },
dateTime: { type: Date, default: Date.now },
comments: [{
userID: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
likes: { type: Number, default: 0 },
}],
})
In react, I want to render just dateTime and likes.
function Post(post) {
const {dateTime, likes } = post;
return (
<tr>
<td>
{dateTime}
</td>
<td>
{likes}
</td>
</tr>
);
}
dateTime works fine and renders the data but Likes does not render anything. I have tried {comments.likes} but an error shows: Error: Objects are not valid as a React child (found: object with keys {dateTime, likes}). If you meant to render a collection of children, use an array instead.
function Post(post) {
const {dateTime, comments.likes } = post;
return (
<tr>
<td>
{dateTime}
</td>
<td>
{comments.likes}
</td>
</tr>
);
}
Does anyone know how to render "likes" so I can display them in react? Thank you so much in advance.