获取对象 #<Object> 在 Ember 中没有方法“map”
Getting Object #<Object> has no method 'map' in Ember
我有一个应用程序.js像这样,
App = Ember.Application.create();
App.Model = Ember.Object.extend({
});
App.IndexRoute = Ember.Route.extend({
redirect : function() {
this.transitionTo('users');
}
});
App.UsersController = Ember.ObjectController.extend({
filteredContent : function() {
var searchText = this.get('searchText'), regex = new RegExp(searchText, 'i');
return this.get('model').filter(function(item) {
return regex.test(item.name);
});
}.property('searchText', 'model')
});
App.User = App.Model.extend({
id : null,
firstname : null,
email : null
});
App.UsersRoute = Ember.Route.extend({
model : function() {
return App.User.findAll();
}
});
App.UserRoute = Ember.Route.extend({
model : function(params) {
//var everyone = this.controllerFor('users');
return App.User.findBy('id', params.user_id);
}
});
App.User.reopenClass({
findAll : function() {
return $.getJSON("http://pioneerdev.us/users/index", function(data) {
return data.map(function(row) {
return App.User.create(row);
});
});
}
});
App.Router.map(function() {
this.resource('users', function() {
this.resource('user',{path: '/:user_id'});
});
});
.HTML
<script type="text/x-handlebars" data-template-name="users">
{{input type="text" value=searchText}}
{{#each item in filteredContent }}
<tr><td>
{{#link-to 'user' item}} <p>{{item.firstname}}</p>{{/link-to}}
</td></tr>
{{/each}}
{{outlet}}
</script>
<script type="text/x-handlebars" data-template-name="user">
<h2>
{{email}}
</h2>
</script>
我在 Chrome 上收到这样的错误,
Uncaught TypeError: Object #<Object> has no method 'map'
火狐给了我,TypeError: data.map is not a function
这是 JSON 的示例,
{"users":[{"id":"1","firstname":"Marilyn","lastname":"Hughes","address":"4 Johnson Alley","state":"Utah","country":"US","email":"lfreeman@skyvu.info","phone":"6471228888","experience":"5","designation":"Writer"},{"id":"2","firstname":"John","lastname":"Porter","address":"86228 Commercial Court","state":"Pennsylvania","country":"US","email":"jporter@zoomdog.net","phone":"9018889877","experience":"3","designation":"Design"},{"id":"3","firstname":"Frances","lastname":"Johnson","address":"489 Summit Lane","state":"Minnesota","country":"US","email":"fjohnson@teklist.net","phone":null,"experience":"2","designation":"Script"}]}
可能是什么问题?我很确定这是.getJSON的问题。
我应该使用data.users.map
而不是data.map
吗?但是当我使用它时,我在这一行上出现错误,
return this.get('model').filter(function(item) {
谚语
Uncaught TypeError: Object #<Object> has no method 'filter'
是的,您需要使用 data.users.map
,因为您需要在控制器的模型属性中包含一个元素数组。
Uncaught TypeError: Object #<Object> has no method 'filter'
错误是因为,this.get('model')
没有data.users
data
。所以Object
类没有一个叫做filter的方法,只是Array
。该数据从$.getJSON().then()
用数据解析的方法返回:
$.getJSON().then(function(data) {
// something like this is made by ember
// controller.set('model', data);
});
你需要data.users作为余烬对象。
所以我将您的代码更新为以下内容:
App.User.reopenClass({
findAll : function() {
return new Ember.RSVP.Promise(function(resolve, reject) {
$.getJSON("http://pioneerdev.us/users/index", function(data) {
var result = data.users.map(function(row) {
return App.User.create(row);
});
// here we resolve the array with ember objects
// so you will have this.get('model') filled with array of users
resolve(result);
}).fail(reject);
});
}
});
使用RSVP.Promise
是个好主意,因为这是 ember 使用的 promise api。
这是显示这个工作 http://jsfiddle.net/marciojunior/HtJEh/的小提琴
相关文章:
- 当我们的设备从纵向模式定向到横向模式时,当地图完全加载时,是否有任何 Map 事件被触发
- 为什么dijit.form.Select有方法setStore(),而dijit.fform.FilteringSele
- 获取对象 #
- JSPM-是否有方法或需要对捆绑文件进行版本化
- 当用户试图删除indexedDB数据库时,是否有方法执行函数
- Box2dWeb中是否有方法将对象的位置.x输出到console.log()
- 是否有方法在WebCrypto API中获取RSA密钥的组件
- jQuery是否有像.map()这样返回对象而不是数组的东西?
- 饼状图显示未捕获类型错误:不能调用方法'map'的定义
- 是否有方法可以快速确定浏览器是否支持启用了cors的图像而不会污染浏览器?
- 是否有方法以编程方式确定私钥是用DER还是PEM编码的?
- 在反序列化JSON对象时,是否有方法将属性名称转换为小写?
- 余烬-对象没有方法'map'
- 是否有方法检查特定点(X,Y)是否在SVG元素中
- 是否有方法减少相同if语句的代码
- 在babel编译时是否有方法注入或忽略全局变量?
- Node.js query:是否有方法提示用户自定义大小的数组
- 在检查字符串时,是否有方法包含单词变体(过去时,动名词)
- 未定义的方法' map': id:象征
- 当元素不处于悬停状态时,是否有方法获得元素的HOVER样式?