Populate() Ngoose only returns Objectid

Asked

Viewed 134 times

1

I imagine it’s something very simple to solve but I’m already several hours trying to solve and I get nowhere.

I’m developing a blog as a way of study but I’m having problems using the populate() of Mongoosis.

I have the following models:

Posts.js

const mongoose = require('mongoose');
const { Schema } = mongoose;

const PostSchema = new Schema({
    title:
    {
        type: String,
        required: true,
    },
    category:
    {
        type: Schema.Types.ObjectId,
        ref: 'categories',
        required: true
    },
    text:
    {
        type: String,
        required: true
    }
})

module.exports = mongoose.model('posts', PostSchema) 

Categories.js

const mongoose = require('mongoose');
const { Schema } = mongoose;

const CategorySchema = new Schema({
    title:
    {
        type: String,
        required: true
    }
})

module.exports = mongoose.model('categories', CategorySchema)

And the code to show all posts made, including the title of the category:

Indexcontroller.js

const Post = require('../models/Posts');

exports.Index = (req, res) =>
{
    Post.find({}).
    populate('categories').
    exec((err, post) =>
    {
        res.json(post);
    })
}

The problem is the only return I get is from id of the category:

[{"_id":"60198923d4323f3630468fce",
"title":"Titulo qualquer",
"category":"60198651822d5147c041eaee",
"text":"Texto qualquer","__v":0}]

Someone could give me a light?

1 answer

0


I think if you’re wrong, you’re in Post.find({}).populate('categories')..., because the correct would be to pass to the method populate the "column", so to speak, that you want popular. You should pass category instead of categories. In this way:

 Post.find({}).
//              ↓↓↓ vamos popular este campo "category"
    populate('category').
    exec((err, post) =>
    {
        res.json(post);
    })

The very Mongoosis documentation gives us an example:

Schemas and models:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const personSchema = Schema({
  _id: Schema.Types.ObjectId,
  name: String,
  age: Number,
// ↓↓↓  propriedade do documento faz referência ao Story.
  stories: [{ type: Schema.Types.ObjectId, ref: 'Story' }]
});

const storySchema = Schema({
// ↓↓↓  propriedade do documento faz referência ao Person. 
  author: { type: Schema.Types.ObjectId, ref: 'Person' },
  title: String,
  fans: [{ type: Schema.Types.ObjectId, ref: 'Person' }]
});

const Story = mongoose.model('Story', storySchema);
const Person = mongoose.model('Person', personSchema);

See that author, name of the document property, is a reference to the Person and when he will do the populate and pass the name of this "column" to the method:

Story.
  findOne({ title: 'Casino Royale' }).
//           ↓↓↓ vamos popular este campo "author"
  populate('author').
  exec(function (err, story) {
    if (err) return handleError(err);
    console.log('The author is %s', story.author.name);
    // prints "The author is Ian Fleming"
  });

Browser other questions tagged

You are not signed in. Login or sign up in order to post.