在对象中填充ObjectId数组将返回[[Object]]

时间:2019-05-22 14:26:56

标签: node.js mongodb mongoose

我正在尝试填充其中包含ObjectID数组的对象。据我了解,我应该像往常一样填充,但是当我控制台记录该对象时,它仅显示[ [Object] ]而不是对象数组。const mongoose = require('mongoose');

这是我的包含数组的模式

const mongoose = require('mongoose');

const partySchema = mongoose.Schema({
    title: { type: String, required: true },
    server: { type: String, required: true },
    leader: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'player'
    },
    tank: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'player'
    }],
    healer: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'player'
    }],
    dps: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'player'
    }]
});

module.exports = mongoose.model('party', partySchema);

这是这些数组内部的架构

const mongoose = require('mongoose');

const playerSchema = mongoose.Schema({
    name: { type: String, required: true },
    server: { type: String, required: true },
    class: { type: Number, required: true },
    role: { type: Number, required: true },
    level: { type: Number, required: true }
});

module.exports = mongoose.model('player', playerSchema);

这是获取数据的代码

const Party = require(__basedir + '/models/party');
const Player = require(__basedir + '/models/player');

var parties = Party
    .find()
    .populate('leader', 'name -_id')
    .populate('tank')
    .then((parties) => {
        console.log(parties);
    })
    .catch((error) => {
        console.err(error);
    });

这是控制台中的结果

[ { tank: [ [Object] ],
    healer: [ 5ce53b801c9d44000037eef9 ],
    dps: [ 5ce53b801c9d44000037eef9 ],
    _id: 5ce5572fe9bd8a25a35a1bad,
    title: 'Party time',
    server: 'Archimonde',
    leader: { name: 'Tariendella' },
    __v: 0 } ]

当我不填充时,在数组中找到任意数量的id都没有问题,但是一旦填充,我就以[Object]结尾,而我似乎无能为力。

我遇到了其他人问同样的问题,而所有人都说只使用填充。

0 个答案:

没有答案