猫鼬填充虚拟的返回值和空数组

时间:2020-10-14 15:54:15

标签: javascript node.js mongoose

我有2个模型:“发布”和“评论”:

const { Schema, model } = require('mongoose')
const mongoosePaginate = require('mongoose-paginate-v2');

const postSchema = new Schema({
    url: String,
    usuario: { type: Schema.ObjectId, ref: 'User' },
    texto: String,
    numLikes: 0,
    numComentarios: 0,
    fechaCreacion: String
})

postSchema.set('toObject', { virtuals: true });
postSchema.set('toJSON', { virtuals: true });

postSchema.virtual('comentarioss', {
    ref: 'Comentario',
    localField: '_id',
    foreignField: 'Post',
    justOne: false,
    options: { sort: { fechaCreacion: 1 } }
  });

postSchema
    .virtual('estaLike')
    .get(function () {
        if (this._estaLike == null) {
            return false;
        }
        return this._estaLike;
    })
    .set(function (v) {
        this._estaLike = v;
    });

postSchema.plugin(mongoosePaginate);

module.exports = model('Post', postSchema);

postSchema.plugin(mongoosePaginate);

module.exports = model('Post', postSchema);

Comentario模型:

const {Schema, model} = require('mongoose')

const comentarioSchema = new Schema({
    post: {type: Schema.ObjectId, ref: 'Post'},
    usuario: {type: Schema.ObjectId, ref: 'User'},
    mensaje: String,
    fechaCreacion: String
})

module.exports = model('Comentario', comentarioSchema)

在我的控制器中,我返回了一个帖子无效”

//3.- Buscando publicaciones de los seguidores (seguidoresClean)            
            const optionsOfPagiante = {
                page: ActualPage,
                limit: 1,
                sort: '-fechaCreacion',
//------------------------------------------------------------------
//I think the problem could be here
//------------------------------------------------------------------
                populate: {
                    path: 'comentarioss',
                    select: '_id Post'
                    //I used the next line but doesn´t work
                    //model: 'Comentario' 
                }
            }
            Post.paginate({ usuario: { $in: seguidoresClean } }, optionsOfPagiante, async (err, result) => {
                console.log('Step 3: ',result.docs[0].comentarioss);
                if (err) {
                    console.log(err);
                    return res.status(500).send({ message: 'Error posts de seguidores' });
                }
                if (result.docs.length === 0) {
                    return res.status(402).send({ message: 'Aún no sigues a alguien, no puedes ver publicaciones' });
                }
                //4.- Agregar el estado "estalike"               
                const estadosConLike = await agregarEstadoLike(userFollowing._id, result);
                var aux = {};
                aux = result;
                delete aux.docs;
                aux.docs = estadosConLike;
                return res.status(200).send(aux);
                //
            });

下一个是我的回复

enter image description here

我的数组“ comentarioss”为空:(。我阅读了文档:https://mongoosejs.com/docs/populate.html#populate-virtuals 但我找不到问题-我已经检查我是否有符合要求的文件

0 个答案:

没有答案