0
votes

I'm using the latest version of ember-data (rev. 11) and the REST adapter to pull data from my API. A sample of the returned JSON looks like this:

{ 
    "events": [
        {
            "id": "5118dd8c4c80866ef2000051",
            "title": null,
            "starts_at": 1361901600,
            "ends_at": null,
            "currency": "SEK",
            "cents_door": 4000,
            "cents_advance": null,
            "price_door": "40.00 kr",
            "price_advance": null,
            "age_limit": null,
            "venue_section": "PLAYHOUSE",
            "description": null,
            "url": null,
            "repeats": null,
            "repeats_until": null,
            "venue_id": "nefertiti-jazz-club",
            "act_ids": [ "marias-playhouse" ]
        }
    ]
}

The model looks like this:

App.Event = DS.Model.extend
  title: DS.attr('string')
  startsAt: DS.attr('number')
  endsAt: DS.attr('number')
  currency: DS.attr('string')
  centsDoor: DS.attr('number')
  centsAdvance: DS.attr('number')
  priceDoor: DS.attr('string')
  priceAdvance: DS.attr('string')
  ageLimit: DS.attr('string')
  venueSection: DS.attr('string')
  description: DS.attr('string')
  url: DS.attr('string')
  repeats: DS.attr('string')
  repeatsUntil: DS.attr('string')
  venue: DS.belongsTo('App.Venue')
  acts: DS.hasMany('App.Act')

But when requesting the data, the request completes successfully, but I get this error in the console:

Uncaught Error: assertion failed: Your server returned a hash with the key events but you have no mapping for it

Any ideas what's going wrong here?

===

UPDATE: As requested I'm adding a bit more of my Ember.js app.

My RESTAdapter setup:

DS.RESTAdapter.registerTransform 'raw',
  deserialize: (serialized) ->
    serialized
  serialize: (deserialized) ->
    deserialized

App.Store = DS.Store.extend
  adapter: DS.RESTAdapter.create
    url: LJ.CONFIG.api.url
  revision: 11

And routes:

App.Router.map ->
  this.resource 'events', ->
    this.route 'new'
  this.resource 'event', path: '/events/:event_id', ->
    this.route 'edit'
  this.resource 'venue', path: '/venues/:venue_id', ->
    this.route 'edit'
    this.resource 'events'
  this.resource 'act', path: '/acts/:act_id', ->
    this.route 'edit'
    this.resource 'events'
  this.route 'search', path: '/search/:term'
  this.route 'doc', path: '/docs/:doc'
2

2 Answers

1
votes

The response looks perfect on first glance.

My guess is that you are sending the wrong format for the wrong request.

This format is valid for many events, which means a findAll or a findQuery (GET /events)

However, you might get this error if you are returning this response for a single find (GET /events/5118dd8c4c80866ef2000051)

In that case (when you are fetching only one event), your response should look like this:

{
  "event": {
    "id": "5118dd8c4c80866ef2000051",
    "title": null,
    // ... rest of attributes
  }
}
1
votes

After much debugging and searching, it seems Ember.js does not support query string parameters yet. I had to instead hack up my routes like this:

App.Router.map ->
  this.resource 'events', path: '/events/:country/:region/:city'
  this.route 'eventsNew', path: '/events/new'
  this.resource 'event', path: '/events/:event_id', ->
    this.route 'edit'
  this.resource 'venue', path: '/venues/:venue_id', ->
    this.route 'edit'
    this.resource 'events'
  this.resource 'act', path: '/acts/:act_id', ->
    this.route 'edit'
    this.resource 'events'
  this.route 'search', path: '/search/:term'
  this.route 'doc', path: '/docs/:doc'

This is far from perfect, but it works for now. Apparently query string support is slated for a near future release.