![](/img/trans.png)
[英]AngularJS: TypeError: Cannot read property 'get' of undefined?
[英]TypeError: Cannot read property 'reduce' of undefined in angularjs
所以我的问题可能有点奇怪,但是我遇到的问题实际上也很奇怪。
因此,为了创建自定义搜索过滤器,在这种情况下,我使用javascript
的.map
和.reduce
函数在我的范围示例中使用了它们。 看下面的代码:
$http.get('config/get/getOrders.php', {cache: true}).then(function(response){
$scope.orders = response.data.orders.order;
$scope.orders.map( function addPlace(item) {
item.firstname = $scope.customers.reduce(function(a,customers){
return item.id_customer === customers.id ? customers.firstname : a;
}, '');
return item;
});
});
因此,在$http.get()
我请求包含数据的JSON文件。 该数据由一些带有信息的订单组成。 所有这些订单都有一个id_customer
值。 此值连接到下订单的客户信息。
因此,我想使用该customer name
扩展搜索功能。有关如何以及为什么看昨天我问过的这个问题的更多信息。
而且有效。 过滤器还可以搜索customer_firstname
。 但是后来我想在另一个控制器中使用相同类型的功能。 功能目标保持不变。 连接来自多个$scope
类型的数据。 但是很奇怪,因为它返回的是TypeError: Cannot read property 'reduce' of undefined
。 是的,我将其放置在相同的app.js中,但仅放置在另一个控制器中。 我已经检查了$scope.products
和$scope.stock_availables
和$scope.productCombiantions
所有数据$scope.productCombiantions
存在并且确实存在,我已经检查了提供者等,并且都适合我,所以我不知道为什么发生。 在这种情况下的功能是:
$http.get('config/get/getProducts.php', {cache: true}).then(function(response){
$scope.products = response.data.products.product;
$scope.products.map( function addPlace(item) {
item.eanCombination = $scope.productCombinations.reduce(function(a, productCombinations, stock_availables){
return item.id === stock_availables.id + stock_availables.id === stock_availables.id_product_attribute + stock_availables.id_product_attribute === productCombinations.id ? productCombinations.ean13: a;
}, '');
return item;
});
});
简短摘要:一个功能在一个控制器中确实起作用,但同一功能在另一个控制器中不起作用。
如果您有任何疑问,请在评论中提问。
一如既往,预先感谢!
所以我在加载所有数据的顺序上犯了一个错误。 因为$scope.productCombinations
是在product序列之后加载的事实。 所以给大家小费。 始终检查编写代码的顺序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.