我正在将ember-rails app的ember部分移植到Ember CLI。到目前为止一切都那么好,但我遇到了一个在RESTSerializer中爆炸的get请求有问题。
Application Controller尝试获取小部件列表
`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正常时需要它?
答案 0 :(得分:1)
当您的服务器返回无效的json响应时,会发生这种情况。在这种情况下,它不会返回根对象。
您的服务器可能会返回类似这样的内容
{
other_id: 1
// ...
}
它必须是
{
unfinished_widget: {
other_id: 1
// ...
}
}
ActiveModelAdapter / Serializer期望它采用那种风格。老实说这个错误很糟糕,应该报告为ember-data中的错误。我以前打过它,调试和追踪非常困难。
答案 1 :(得分:1)
因此,事实证明,它只是EmberData 1.0.0.beta.10中的一个错误。我更新到测试版12,一切正常。