简体   繁体   中英

TypeError: Cannot read property 'forEach' of undefined Ember-Data

When I hit '/' of my app, I am getting the stack trace below

Error while loading route: TypeError: Cannot read property 'forEach' of undefined
    at Function.Model.reopenClass.eachAttribute (http://localhost:3000/assets/ember-data.js?body=1:4870:32)
    at JSONSerializer.extend.normalizeAttributes (http://localhost:3000/assets/ember-data.js?body=1:2906:16)
    at JSONSerializer.extend.normalize (http://localhost:3000/assets/ember-data.js?body=1:2827:14)
    at superWrapper (http://localhost:3000/assets/ember.js?body=1:1293:16)
    at superFunction [as _super] (http://localhost:3000/assets/ember.js?body=1:7724:16)
    at RESTSerializer.extend.normalize (http://localhost:3000/assets/ember-data.js?body=1:378:21)
    at superWrapper [as normalize] (http://localhost:3000/assets/ember.js?body=1:1293:16)
    at null.<anonymous> (http://localhost:3000/assets/ember-data.js?body=1:3179:35)
    at Array.map (native)
    at JSONSerializer.extend.extractArray (http://localhost:3000/assets/ember-data.js?body=1:3178:37) 

Relevant Code is Here (in Coffeescript):

Plnnr.ApplicationRoute = Ember.Route.extend(
  model: ->
    @store.find('stage')
)

Plnnr.Stage = DS.Model.extend(
  tasks:        DS.hasMany("task")
  name:         DS.attr("string")
  description:  DS.attr("string")
  position:     DS.attr("number")
)

Plnnr.ApplicationAdapter = DS.ActiveModelAdapter.extend(
  namespace: 'v1'
) 

The API is setup with Rails Serializer and setting a breakpoint shows that the Adapter is successfully retrieving the data.

I also set a breakpoint in Ember-data.js, at the origin of where the failure starts (when .normalize is called in the code below):

var normalizedArray = map.call(payload[prop], function(hash) {
  return typeSerializer.normalize(type, hash, prop);
}, this);

At that time, type = DS.Model and hash = the serialized API payload.

I'm new to Ember and arent familiar with how to interpret the documentation. Does anyone know what could be wrong, and have any suggestions on how I can trace the problem?

Thanks!

It turns out the problem was because I was using Coffeescript with Ember.

I declared my ember Task class like so:

class Plnnr.Task extends DS.Model

instead of

Plnnr.Task = DS.Model.extend

These two are NOT equivalent. While Stage correctly used the second convention, Task (which belongs to Stage) incorrectly used the first convention. When the JSON came back with both Stage and Task objects, the serializer threw up the trace above, because it couldn't handle Plnnr.Task parsing correctly

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM