【问题标题】:ember-cli - Ember Data get request failed TypeError - undefined is not a functionember-cli - Ember 数据获取请求失败 TypeError - 未定义不是函数
【发布时间】:2014-10-31 14:37:48
【问题描述】:

我正在将 ember-rails 应用程序的 ember 部分移植到 Ember CLI。到目前为止一切顺利,但我遇到了在 RESTSerializer 中爆炸的获取请求。

应用程序控制器尝试获取小部件列表

`import Ember from 'ember'`
ApplicationController = Ember.Controller.extend
  widgets: (->
    @store.find('unfinishedWidgets', id: @get('currentId'))
  ).property()

`export default ApplicationController`

我可以看到 API 请求触发,JSON 回来了,一切看起来就像在 Ember Rails 堆栈中所做的一样。除了更新属性并在视图中显示之外,它会爆炸:

 TypeError: undefined is not a function
        at Object.func (http://localhost:4200/myapp/assets/vendor.js:49473:18)
        at Object.Cache.get (http://localhost:4200/myapp/assets/vendor.js:25091:38)
        at decamelize (http://localhost:4200/myapp/assets/vendor.js:49515:31)
        at RESTSerializer.extend.keyForAttribute (http://localhost:4200/myapp/assets/vendor.js:66565:16)
        at apply (http://localhost:4200/myapp/assets/vendor.js:32821:27)
        at superWrapper [as keyForAttribute] (http://localhost:4200/myapp/assets/vendor.js:32393:15)
        at null.<anonymous> (http://localhost:4200/myapp/assets/vendor.js:69024:31)
        at null.<anonymous> (http://localhost:4200/myapp/assets/vendor.js:71513:20)
        at cb (http://localhost:4200/myapp/assets/vendor.js:29067:22)
        at OrderedSet.forEach (http://localhost:4200/myapp/assets/vendor.js:28865:13) vendor.js:28532logToConsole vendor.js:28532RSVP.onerrorDefault vendor.js:42608__exports__.default.trigger vendor.js:61072Promise._onerror vendor.js:62084publishRejection vendor.js:60315(anonymous function) vendor.js:42583DeferredActionQueues.invoke vendor.js:13853DeferredActionQueues.flush vendor.js:13923Backburner.end vendor.js:13309Backburner.run vendor.js:13364run vendor.js:31375hash.success vendor.js:68006fire vendor.js:3237self.fireWith vendor.js:3349done vendor.js:9393callback

我在 decamelize 处设置了一个断点,它停止了,以便我可以检查发生了什么:

function decamelize(str) {
  return DECAMELIZE_CACHE.get(str);
}

此时的str不是字符串,它是:

Object {type: undefined, isAttribute: true, options: Object, parentType: function, name: "bundleId"}
  isAttribute: true
  name: "otherId"
  options: Object
  parentType: (subclass of DS.Model)
  type: undefined__proto__: Object

所以这是我模型中的第一个 DS.attr():

`import DS from 'ember-data'`
unfinishedWidgets = DS.Model.extend
  otherId: DS.attr()
  # other attrs
`export default UnsubmittedRequest`

我默认使用的是 ActiveModelAdapter,并且我还创建了一个空的 ActiveModelSerializer。

`import DS from 'ember-data'`
ApplicationAdapter = DS.ActiveModelAdapter.extend
  namespace: 'api/myapp/v1' 
`export default ApplicationAdapter`

`import DS from 'ember-data'`
ApplicationSerializer = DS.ActiveModelSerializer.extend()
`export default ApplicationSerializer`

编辑:

我最终修复了它:

ApplicationSerializer = DS.ActiveModelSerializer.extend
  keyForAttribute: (type, name) ->
    name

虽然我仍然不清楚为什么在 Ember CLI 中它在 Ember-rails 中很好时是必要的

【问题讨论】:

    标签: coffeescript ember-data ember-cli


    【解决方案1】:

    当您的服务器返回无效的 json 响应时会发生这种情况。在这种情况下,它不会返回根对象。

    您的服务器可能会返回类似这样的内容

    {
      other_id: 1
      // ...
    }
    

    它必须是

    {
      unfinished_widget: {
        other_id: 1
        // ...
      }
    }
    

    ActiveModelAdapter/Serializer 期望它采用这种风格。老实说,这个错误很糟糕,应该在 ember-data 中报告为错误。我以前打过它,调试和追踪非常困难。

    【讨论】:

    • 肯定不是,一切都按预期用AMS格式化,包括你这里提出的具体想法。据我所知,我确实设法在配置的尖峰应用程序上使用了这个 API。
    【解决方案2】:

    因此,事实证明,这只是 EmberData 1.0.0.beta.10 中的一个错误。我更新到 beta 12,一切正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-27
      • 1970-01-01
      相关资源
      最近更新 更多