填充猫鼬返回未定义

时间:2020-10-09 21:07:49

标签: node.js mongodb mongoose

我有两个收藏集video和videoCategory

视频:

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;

和视频类别

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;

我保存了一个视频,我的文档是:

/* 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
}

当我执行查询时,控制台日志中收到未定义的消息

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);

我阅读了文档,但我不知道我的错误在哪里。

0 个答案:

没有答案