猫鼬人口返回一个空数组

时间:2019-01-02 11:43:32

标签: node.js mongoose

我有一个问题,当尝试使用Model.Populate()进行填充时,该函数只会返回一个空数组,例如:[],而不是这样的对象数组:[{apartment Apartment},.... ]。

客户端架构:

const mongoose = require('mongoose');
const bcrypt = require('bcrypt');
const Schema = mongoose.Schema;
const clientSchema = mongoose.Schema({
_id: Schema.Types.ObjectId,
email: { type: String, required: true },
password: { type: String, required: true},
apartments: [{type: Schema.Types.ObjectId, ref: 'Apartment'}]
});
const Client = module.exports = mongoose.model('Client', clientSchema);

公寓架构:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const apartmentSchema = mongoose.Schema({
_id: Schema.Types.ObjectId,
user_id: { type: Schema.Types.ObjectId, ref: 'Client'},
price: { type: Number,required: true },
city: { type: String, required: true },
street: { type: String, required: true }
});

const Apartment = module.exports = 
mongoose.model('Apartment',apartmentSchema);

GET方法:

    const findAll = (req, res) => {
    Client.find()
    .then(clients => Client.populate(clients, {
        path: 'apartments'
    }))
    .then(clients => res.status(200).send(clients))
    .catch(err => res.status(500).json(err));
    };

我什至浏览了许多建议的解决方案,看了看文档,但是仍然没有发现任何错误,但是似乎有问题,因为它没有用。感谢您的帮助!

0 个答案:

没有答案
相关问题