2016-08-17 7 views
0

モングースの人口機能に問題があります。問題は連絡先がソートされていないため、私が使っているクラブに関係なく、私はそれらのすべてを手に入れることです。人口はモンゴセでは働いていません

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

    }); 

clubDetail.js角度で(私はRestangularを使用しています)

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

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

チェックあなたrefコレクション名はデータベース内の 'Club'と全く同じです –

+0

module.exports = mongoose.model( 'Club'、clubSchema); 'clubs'と 'club'の両方に変更しようとしましたが、エラーが表示されます:MissingSchemaError :モデル "club"のスキーマは登録されていません –

+0

結果はどのように見えますか? –

答えて

0

は、私は最終的に答えを見つけました。問題は私がクラブを定義していないことでした。

Contactperson.find({クラブ:req.params.club_id})ここで正しいfindメソッドである。.populate( 'クラブ')のexec(関数(ERR、contactpersons){場合

+0

うまくいってくれてうれしいですが、その変更は人口を動かすことと何が関係していますか?返されたドキュメントを 'club'と一致するものだけにフィルタリングするのではないでしょうか? – JohnnyHK

+0

はい、それは私が達成しようとしていたものでした。それはあなたが人口のために使用するものではありませんか? –

+0

いいえ、[population](http://mongoosejs.com/docs/populate.html)は、元のContactpersonクエリを参照先(Club)モデルに追加して、参照されたクラブ文書を取得する代わりに、 contactpersonの 'club' ObjectIdをdocに置き換えます。 – JohnnyHK

関連する問題