[英]EmberJS users str.replace is not a function TypeError: str.replace is not a function
所以我只是在玩EmberJS,我真的很想學習EmberJS,但是我一直陷在這個問題上。 我正在進行api調用,我想我已經按照EmberJS RESTAdapter
要求的方式格式化了json,但我不確定。
我的路由器:
App.Router.map( function() {
this.route('about', { path: '/about' });
this.route('users', { path: '/users' });
});
我的用戶路線:
App.UsersRoute = Ember.Route.extend({
model: function() {
/*
this.store.findAll('user').then( function( results ) {
console.log( results );
}, function() {
console.log('failed');
});
*/
return this.store.findAll('user');
}
});
我的app.js
var App = Ember.Application.create();
App.ApplicationAdapter = DS.RESTAdapter.extend({
host: 'http://api.diverseevolution.com'
});
App.ApplicationSerializer = DS.RESTSerializer.extend({
primaryKey: 'id',
serializeId: function( id ) {
return id.toString();
}
});
我的模板:
<script type="text/x-handlebars" data-template-name="users">
<p>This is the users route, the goal is to plugin the users fixtures.</p>
<table class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>First name:</th>
<th>Last name:</th>
</tr>
</thead>
<tbody>
{{#each model as |user|}}
<tr>
<td>{{ user.id }}</td>
<td>{{ user.first_name }}</td>
<td>{{ user.last_name }}</td>
</tr>
{{else}}
<tr>
<td colspan="3">No users found...</td>
</tr>
{{/each}}
</tbody>
</table>
</script>
我的模特:
App.User = DS.Model.extend({
firstName: DS.attr('string'),
lastName: DS.attr('string'),
email: DS.attr('string')
});
最后是服務的我的json響應:
{
"users":[
{
"id":"1",
"firstName":"Thomas",
"lastName":"Bird",
"address":"4035 Stonebridge",
"city":"Holly",
"state":"MI",
"zip":"48442",
"email":"godoploid@gmail.com",
"status":{
"id":"1",
"title":"active",
"created_at":"-0001-11-30 00:00:00",
"updated_at":"-0001-11-30 00:00:00",
"deleted_at":null
},
"userTypeId":"1",
"createdAt":"-0001-11-30 00:00:00",
"updatedAt":"-0001-11-30 00:00:00",
"deletedAt":null,
"type":{
"id":"1",
"title":"Admin",
"created_at":"-0001-11-30 00:00:00",
"updated_at":"-0001-11-30 00:00:00",
"deleted_at":null
}
},
{
"id":"2",
"firstName":"Avery",
"lastName":"Crain",
"address":"282 Mansfield Dr.",
"city":"Lapeer",
"state":"MI",
"zip":"48446",
"email":"avery.crain@gmail.com",
"status":{
"id":"1",
"title":"active",
"created_at":"-0001-11-30 00:00:00",
"updated_at":"-0001-11-30 00:00:00",
"deleted_at":null
},
"userTypeId":"2",
"createdAt":"-0001-11-30 00:00:00",
"updatedAt":"-0001-11-30 00:00:00",
"deletedAt":null,
"type":{
"id":"2",
"title":"User",
"created_at":"-0001-11-30 00:00:00",
"updated_at":"-0001-11-30 00:00:00",
"deleted_at":null
}
},
{
"id":"3",
"firstName":"Mike",
"lastName":"Marshall",
"address":"4890 Lawndale St.",
"city":"North Branch",
"state":"MI",
"zip":"48461",
"email":"mjmarshall@gmail.com",
"status":{
"id":"1",
"title":"active",
"created_at":"-0001-11-30 00:00:00",
"updated_at":"-0001-11-30 00:00:00",
"deleted_at":null
},
"userTypeId":"2",
"createdAt":"-0001-11-30 00:00:00",
"updatedAt":"-0001-11-30 00:00:00",
"deletedAt":null,
"type":{
"id":"2",
"title":"User",
"created_at":"-0001-11-30 00:00:00",
"updated_at":"-0001-11-30 00:00:00",
"deleted_at":null
}
},
{
"id":"4",
"firstName":"Kylar",
"lastName":"Bird",
"address":"5875 Gracelawn",
"city":"North Branch",
"state":"MI",
"zip":"48461",
"email":"kbird@gmail.com",
"status":{
"id":"1",
"title":"active",
"created_at":"-0001-11-30 00:00:00",
"updated_at":"-0001-11-30 00:00:00",
"deleted_at":null
},
"userTypeId":"2",
"createdAt":"-0001-11-30 00:00:00",
"updatedAt":"-0001-11-30 00:00:00",
"deletedAt":null,
"type":{
"id":"2",
"title":"User",
"created_at":"-0001-11-30 00:00:00",
"updated_at":"-0001-11-30 00:00:00",
"deleted_at":null
}
}
]
}
實時api可以在這里找到: http : //api.diverseevolution.com/users
這是我得到的錯誤:
我一生都無法弄清楚為什么仍然會收到此錯誤,我將api更改為Ember文檔中描述的多種格式,我也更改了序列化器,適配器,但仍然無法弄清楚為什么我遇到此錯誤,如果有人可以提供幫助,我將非常感激。 我真的想盡全力圍繞EmberJS,但是最近兩天我一直在這里停留!
謝謝
看來我無法重現您的錯誤,並且在您看來:
firstName
而不是first_name
拼寫錯誤 ApplicationSerializer
(與默認設置沒有什么不同) 您的代碼將起作用。
看來EmberJS不喜歡我從api發回的多個關系。
{
"id":"3",
"firstName":"Mike",
"lastName":"Marshall",
"address":"4890 Lawndale St.",
"city":"North Branch",
"state":"MI",
"zip":"48461",
"email":"mjmarshall@gmail.com",
"status":{
"id":"1",
"title":"active",
"created_at":"-0001-11-30 00:00:00",
"updated_at":"-0001-11-30 00:00:00",
"deleted_at":null
},
"userTypeId":"2",
"createdAt":"-0001-11-30 00:00:00",
"updatedAt":"-0001-11-30 00:00:00",
"deletedAt":null,
"type":{
"id":"2",
"title":"User",
"created_at":"-0001-11-30 00:00:00",
"updated_at":"-0001-11-30 00:00:00",
"deleted_at":null
}
}
一旦我擺脫了所有問題,便會具體說明status
和type
。 我現在的響應看起來像這樣,效果很好:
{
"id":"1",
"firstName":"Thomas",
"lastName":"Bird",
"address":"123 Somewhere Rd",
"city":"Somewhere",
"state":"MI",
"zip":"98675",
"email":"godoploid@gmail.com",
"status":"1",
"userTypeId":"1",
"createdAt":"-0001-11-30 00:00:00",
"updatedAt":"-0001-11-30 00:00:00",
"deletedAt":null
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.