How query API to get all the data for third level of path?

前端 未结 1 380
一整个雨季
一整个雨季 2021-01-25 18:08

This is current schema i have:

var Schema = mongoose.Schema;

    var ProviderSchema = new Schema({
        name: String,
        abbreviated: String,
        se         


        
相关标签:
1条回答
  • 2021-01-25 18:50

    Given your schema and route I assume you have a model called Provider that uses the schema shown and that :provider in the URL is the ID for the requested provider. If so you could use findById. Also, since you are using an array of references you will need to populate them if you want anything other than their IDs:

    router.get('/providers/:provider/posts', function(req, res) {
      Provider.findById(req.params.provider).select('posts').populate('posts').exec(function(err, provider) {
        if(err){ return next(err); }
        res.json(provider.posts);
      });
    });
    

    Otherwise if you are trying to use the Post model you need to show that schema.

    UPDATE: Using your Post model:

    router.get('/providers/:provider/posts', function(req, res) {
      Post.find({provider: req.params.provider}, function(err, posts) {
        if(err){ return next(err); }
        res.json(posts);
      });
    });
    
    0 讨论(0)
提交回复
热议问题