2014-10-31 28 views
0

我將ember-rails應用程序的餘燼部分移植到Ember CLI。到目前爲止這麼好,但是我在RESTSerializer中發出的獲取請求遇到問題。ember-cli - Ember數據獲取請求失敗TypeError - undefined不是函數

應用控制器試圖抓住小部件

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

`export default ApplicationController` 

可以看我的API請求關火的列表中,JSON回來,一切看起來就像它在灰燼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 

雖然我仍然在爲什麼它在灰燼CLI是必要的,當它在灰燼護欄被罰款不明確

回答

1

所以,事實證明,這只是EmberData 1.0.0.beta.10中的一個錯誤。我更新到測試版12,一切正常。

1

當您的服務器返回無效的json響應時,會發生這種情況。在這種情況下,它不會返回一個根對象。

你服務器可能返回這樣的事情

{ 
    other_id: 1 
    // ... 
} 

和它需要

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

的ActiveModelAdapter /串行期望它在風格。老實說這個錯誤是可怕的,應該被報告爲一個燼數據中的錯誤。我以前就碰到過它,調試和追蹤非常困難。

+0

這絕對不是,所有事情都按照預期與AMS格式化,包括您在此提出的具體想法。我確實設法在配置的尖峯應用上使用此API,據我所知,完全相同。 – DVG 2014-12-01 20:12:32

+0

有一個upvote爲你的時間:) – DVG 2014-12-01 20:49:09

相關問題