[英]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.