2017-07-04 70 views
1

我有模型和序列化程序的問題。我查看了文檔並找不到解決方案。我的應用程序連接到django rest api。「str未定義」在findRecord

Django的響應:

{ 
    "id": 1, 
    "url": "http://localhost:8000/users/1/", 
    "username": "username", 
    "email": "[email protected]", 
    "is_staff": true 
} 

模型:

export default DS.Model.extend({ 
    url : DS.attr('string'), 
    username : DS.attr('string'), 
    email : DS.attr('string'), 
}); 

我修改與串行器的響應包括 「數據」:

export default DS.JSONAPISerializer.extend({ 
    primaryKey: 'id', 
    normalizeFindRecordResponse(store, type, payload, id) { 
     console.log('payload',payload) 
     return { 
      data: { 
       'id': id, 
       'url': payload.url, 
       'username': payload.username, 
       'email': payload.email, 
      } 
     } ; 
    } 
}); 

路線:

export default Ember.Route.extend({ 
    model() { 
     return this.store.findRecord('users', 1); 
    } 
}); 

路線相同的錯誤的另一個版本:

export default Ember.Route.extend({ 
    model() { 
     this.store.findRecord('users', '1').then(function(user){ 
      console.log('user', user); 
     }).catch(function(e){ 
      console.log('e', e); 
     }); 
    }  
}); 

最後灰燼檢查有模式,但所有數值爲未定義和「str是未定義」在控制檯上。

Image of ember inspector

回答

0

1. normalizeFindRecordResponse沒有返回有效JSONAPI格式,下面 是有效的格式,

{ 
    "data": { 
    "type": "articles", 
    "id": "1", 
    "attributes": { 
     // ... this article's attributes 
    }   
    } 
} 

正如你所看到的,你缺少typeattributes關鍵。

2.您的模型文件名應爲user.js單數。
3. this.store.findRecord('users', 1) - 在這裏你需要使用模型singluar形式,以便this.store.findRecord('user', 1)
4.In你你缺少模型掛鉤return語句的又一次嘗試。

+1

第一點解決了這個問題。謝謝你的幫助。 – carloscba