1

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?

Cory Danielson
  • 14,314
  • 3
  • 44
  • 51
Udders
  • 6,914
  • 24
  • 102
  • 194

2 Answers2

3

Use findWhere instead. Docs

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

aleha_84
  • 8,309
  • 2
  • 38
  • 46
2

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')
Community
  • 1
  • 1
user229044
  • 232,980
  • 40
  • 330
  • 338
  • Thanks is there a way to turn the array into a model? – Udders Oct 22 '14 at 12:04
  • @Udders "Turn an array into a model"? No, you just need to get the model out of the array. My second option specifically says `organisation = organisation[0]`. – user229044 Oct 22 '14 at 12:06
  • @Udders in this case you should check `organisation.length != 0` first or you risk to get exception – aleha_84 Oct 22 '14 at 19:16