填充不在Mongoose中工作

时间:2016-08-17 11:57:21

标签: angularjs node.js mongodb mongoose restangular

我在使用Mongoose中的populate函数时遇到了一些麻烦。问题是联系人没有被排序,所以我得到了所有这些,无论我使用的是哪个俱乐部。

models.js

// Contactperson Schema
var contactPersonSchema = new Schema({
    club: {
        type: Schema.ObjectId,
        ref: 'Club'
    },
    name: String,
    phoneNumber: String,
    email: String,
    position: String,
    comment: String
});


// Club Schema
var clubSchema = new Schema({
    name: String,
    postalPlace: String,
    comment: String,
    status: String
});

routes.js

router.use('/api/clubs', clubRoute);

clubroutes.js

router.route('/:club_id/contactpersons')
    .post(function (req, res) {
        var contactperson = new Contactperson();
        contactperson.club = req.body.club;
        contactperson.name = req.body.name;
        contactperson.phoneNumber = req.body.phoneNumber;
        contactperson.email = req.body.email;
        contactperson.position = req.body.position;
        contactperson.comment = req.body.comment;

        contactperson.save(function (err) {
            if (err) {
                res.send(err);
            }

            res.json({message: 'Contactperson created!'});
        });
    })
    .get(function (req, res) {
        console.log(req);
        Contactperson.find({}).populate('club').exec(function (err, contactpersons) {

                if (err) {
                    res.send(err);
                }

                res.json(contactpersons);
            });

    });
Angular中的

clubDetail.js (我使用Restangular)

$scope.club = clubService.one($routeParams.id).get().$object;

$scope.contactpersons = clubService.one($routeParams.id).all('contactpersons').getList().$object;

1 个答案:

答案 0 :(得分:0)

我终于找到了答案。问题是我没有定义俱乐部。这是正确的查找方法:

Contactperson.find( {club:req.params.club_id} )。populate(' club')。exec(function(err,contactpersons){

相关问题