Udders
Udders

Reputation: 6976

Backbone collection.where

I am retrieving a model from a backbone collection.

var organisation = this.collection.where({ group_id : String(elm.data('groupid')) });

this returns a result as I would expect it too.

I then when to do some getting and setting on that model, but if I try and run,

organisation.get('members')

then I get the following error message,

Uncaught TypeError: undefined is not a function

I am assuming (maybe wrongly) that is because where() does not acutally return model?

If that is the case then how can I cast the returned data into a model?

Upvotes: 1

Views: 4191

Answers (2)

user229044
user229044

Reputation: 239240

From the docs:

where

collection.where(attributes)

Return an array of all the models in a collection that match the passed attributes. Useful for simple cases of filter

You're getting an array of records back. If you want to work with one of the returned records, either:

organisation[0].get('members')

or

organisation = organisation[0];
organisation.get('members')

Upvotes: 2

aleha_84
aleha_84

Reputation: 8539

Use findWhere instead. Docs

Just like where, but directly returns only the first model in the collection that matches the passed attributes

Upvotes: 3

Related Questions