Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find on Schema only ever returns 1 record #47

Closed
alefort opened this issue Apr 20, 2015 · 10 comments
Closed

Find on Schema only ever returns 1 record #47

alefort opened this issue Apr 20, 2015 · 10 comments

Comments

@alefort
Copy link

alefort commented Apr 20, 2015

I am running a find on a schema, and when I run the find command in Mongo (via Robomongo for testing), I get all 112 records....when I do a find on the same schema in Mers, I only get 1 record instead of the 112.

I suspect I am missing something in my code, but I don't know.

Mongo find:

db.getCollection('inventories').find({store_id: 112});

and my mers code:

var inventorySchema = models.schema.inventory;

inventorySchema.statics.findByStoreId = function findByStoreId(q, term){
    return this.find({'store_id': parseInt(term)});
}
mongoose.model('inventory', inventorySchema);
@jspears
Copy link
Owner

jspears commented Apr 20, 2015

I will need to double check, but my guess is I am defaulting to single results for statics. I should do something smarter but, if you add a query parameter single=false it should work. I am thinking I should add a function configuration for this, because its a little hard to determine...

var inventorySchema = models.schema.inventory;

inventorySchema.statics.findByStoreId = function findByStoreId(q, term){
    return this.find({'store_id': parseInt(term)});
}
inventorySchema.statics.findByStoreId.single = false
mongoose.model('inventory', inventorySchema);

I will make that work shortly. But try the first suggestion.

@alefort
Copy link
Author

alefort commented Apr 20, 2015

Justin,

Thanks for the ever speedy response.

How would I change my static to include that query parameter? I wasn't able
to find an example on your project page.

Cheers

Andre LeFort

On Mon, Apr 20, 2015 at 1:50 PM, Justin notifications@github.com wrote:

I will need to double check, but my guess is I am defaulting to single
results for statics. I should do something smarter but, if you add a query
parameter single=false it should work. I am thinking I should add a
function configuration for this, because its a little hard to determine...

var inventorySchema = models.schema.inventory;
inventorySchema.statics.findByStoreId = function findByStoreId(q, term){
return this.find({'store_id': parseInt(term)});
}
inventorySchema.statics.findByStoreId.single = false
mongoose.model('inventory', inventorySchema);

I will make that work shortly. But try the first suggestion.


Reply to this email directly or view it on GitHub
#47 (comment).

@jspears
Copy link
Owner

jspears commented Apr 20, 2015

@alefort
Copy link
Author

alefort commented Apr 20, 2015

Hmm, that still returns only 1 record. Here is my url

http://192.168.33.11:3000/rest/inventory/finder/findByStoreId/112?single=false

@jspears
Copy link
Owner

jspears commented Apr 20, 2015

sugar, than its a bug. I'll try to get to it tonight.

@alefort
Copy link
Author

alefort commented Apr 20, 2015

Thanks Justin.

@jspears
Copy link
Owner

jspears commented Apr 20, 2015

So I just wrote a test, that I thought would fail but it doesn't... What version are you using?

@alefort
Copy link
Author

alefort commented Apr 20, 2015

Justin, Let's mark this complete, I looked at my code with fresh eyes and noticed a silly typo.

Cheers for the help! All works well, as per your examples in this thread.

@jspears
Copy link
Owner

jspears commented Apr 20, 2015

Ahh... I think I found it ..
My new injection thingy, keeps trying to descend the return. to keep that from
happening you need to stop descending by removing the param from the list.

Here is an example (soon to be added to mers documentation by some endeavoring soul, or me).

 BlogPostSchema.statics.findTitleLikeInject = function findTitleLike(params$) {
        //shifting the param keeps it from from being descended.
        var search = params$.shift();
        return this.find({title: new RegExp(search, 'i')});
    }

Let me know if this works for you.

@jspears
Copy link
Owner

jspears commented Apr 20, 2015

yay, not a bug!

@jspears jspears closed this as completed Apr 20, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants