1

This is current schema i have:

var Schema = mongoose.Schema;

    var ProviderSchema = new Schema({
        name: String,
        abbreviated: String,
        services: Array,
        locations: Array,
        description: String,
        url: String,
        upvotes:{type:Number, default:0},
        createdOn: { type: Date, default: Date.now},
        posts:[{ type: mongoose.Schema.Types.ObjectId, ref: 'Post' }]
    });

This is post schema:

var PostSchema = new mongoose.Schema({
     title: String,
     body: String,
     upvotes: {type: Number, default:0},
     author: String,
     provider: { type: mongoose.Schema.Types.ObjectId, ref: 'Provider' },
     comments:[{ type: mongoose.Schema.Types.ObjectId, ref: 'Comment' }]
 });

I populated post before:

router.get('/providers/:provider', function(req, res) {
    req.provider.populate('posts', function(err, provider) {
        res.json(provider);
    });
});

And this is what i have in my API:

router.get('/providers/:provider/posts', function(req, res) {
    Post.find(function(err, post){
        if(err){ return next(err); }
        res.json(post);
    });
});

I would like to write a query in API which query all the posts for one provider. This router.get is not working and give back me wrong answer.Please if somebody can help me to change this query to correct one. I will be appreciated.

pm1359
  • 622
  • 1
  • 10
  • 31

1 Answers1

1

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);
  });
});
Jason Cust
  • 10,743
  • 2
  • 33
  • 45
  • I update my question. For previous answer i got error **TypeError: Cannot read property 'posts' of null** from line `res.json(provider.posts);` I like to add i don't have any front-end and i am using curl for testing. @Jason Cust – pm1359 Apr 24 '15 at 19:22
  • I have no idea how this works `req.provider.populate(...)` unless you have some custom middleware pre-fetching the provider and attaching that property to the req object. See update for both. – Jason Cust Apr 24 '15 at 19:42
  • That's cool. Provider model is working now. However post model doesn't give back correct answer and only gave back first member of array. That's strange. I use the first model.Thank you for your answer. @Jason Cust – pm1359 Apr 24 '15 at 19:58
  • Apologies, I forgot to clean up the query. `Post` should work now. – Jason Cust Apr 24 '15 at 20:04
  • Can you help me to solve this post [link](http://stackoverflow.com/questions/29917713/api-for-delete-function-is-not-working-anymore-and-doesnt-remove-anything-from) – pm1359 Apr 28 '15 at 12:57