Populate mongoose return undefined

Viewed 21

I have two collections video and videoCategory

Video:

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

const videoSchema = new mongoose.Schema({
    videoUrl: {
        type: String,
        unique: true
    },
    titolo: String,
    descrizione: String,
    categoria: [{ type: Schema.Types.ObjectId, ref: 'videoCategory' }]
}, {timestamps: true});

const Video = mongoose.model('Video', videoSchema);

module.exports = Video;

And video category

const mongoose = require('mongoose');

const videoCategorySchema = new mongoose.Schema({
    titolo: {
        type: String,
        unique: true
    },
    descrizione: String,
}, {timestamps: true});

const videoCategory = mongoose.model('videoCategory', videoCategorySchema);

module.exports = videoCategory;

I save a video and my document is:

/* 1 */
{
    "_id" : ObjectId("5f80cd78a6b9b82aef2e6d14"),
    "categoria" : [ 
        ObjectId("5f80a30675c2f01b2c56d627")
    ],
    "videoUrl" : "https://player.vimeo.com/video/420483626",
    "titolo" : "32131232132",
    "descrizione" : "dddd",
    "createdAt" : ISODate("2020-10-09T20:52:08.317Z"),
    "updatedAt" : ISODate("2020-10-09T20:52:08.317Z"),
    "__v" : 0
}

When I do a query a receive undefined in my console log

await Video.
findOne({ videoUrl: param.videoUrl }).
populate('videoCategory').
exec(function (err, video) {
    if (err) return handleError(err);
    console.log('The titolo is %s', video.categoria[0].titolo);

I read the documentation but I don't understand where is my error.

0 Answers
Related