我正在构建一个词汇应用程序,可以根据作者,书籍,标签等对单词进行排序。下面是我的JSON示例:
{
"expression": "mithrandir",
"meaning": "language of the elves",
"example": ["mithrandir is cool", "the elves speak mithrandir"],
"pronunciation": "",
"notes": "",
"meta": {
"book": ["There and back again"],
"author": ["Frodo Baggins"],
"tags": ["middle earth", "elves"]}
},
我在视图中有三个下拉列表来过滤json中的agsinst book,author和tags属性。现在前面提到的列表正在加载abs,直到我将json移动到firebase。
这是我的工厂:
angular
.module("ngClassifieds")
.factory("classifiedsFactory", function($http, $firebaseArray){
var ref = new Firebase('https://PATH.firebaseio.com/');
return {
ref: $firebaseArray(ref)
}
});
})();
foll是我的控制者:
angular
.module("ngClassifieds")
.controller("classifiedsCtrl", function($scope, $state, $http, classifiedsFactory, $mdSidenav, $mdToast, $mdDialog) {
$scope.classifieds = classifiedsFactory.ref;
$scope.classifieds.$loaded().then(function(classifieds) {
$scope.tags = getTags(classifieds); // call the getTags method below
$scope.books = getBooks(classifieds); // call the getBooks method below
$scope.authors = getAuthors(classifieds); // call the getAuthors method below
$scope.order = ""; //for sorting in asc or desc order
});
'tags','books'和'authors'是用于捕获unqique属性的数组。 foll是getTags()方法的一个例子。
function getTags(classifieds) {
var tags = [];
angular.forEach(classifieds, function(item) {
angular.forEach(item.meta.tags, function(tag) {
tags.push(tag);
});
});
return _.uniq(tags);
}
foll是我在chrome中的错误:
TypeError: Cannot read property 'tags' of undefined
at classifieds.ctr.js:135
at Object.forEach (angular.js:321)
at getTags (classifieds.ctr.js:134)
at classifieds.ctr.js:20
at processQueue (angular.js:15552)
at angular.js:15568
at Scope.$eval (angular.js:16820)
at Scope.$digest (angular.js:16636)
at angular.js:16859
at completeOutstandingRequest (angular.js:5804)
以下是firefox中的错误:
Error: item.meta is undefined
getTags/<@http://localhost:8080/components/classifieds/classifieds.ctr.js:135:5
forEach@http://localhost:8080/node_modules/angular/angular.js:321:11
getTags@http://localhost:8080/components/classifieds/classifieds.ctr.js:134:4
@http://localhost:8080/components/classifieds/classifieds.ctr.js:20:18
processQueue@http://localhost:8080/node_modules/angular/angular.js:15552:28
scheduleProcessQueue/<@http://localhost:8080/node_modules/angular/angular.js:15568:27
$RootScopeProvider/this.$get</Scope.prototype.$eval@http://localhost:8080/node_modules/angular/angular.js:16820:16
$RootScopeProvider/this.$get</Scope.prototype.$digest@http://localhost:8080/node_modules/angular/angular.js:16636:15
$RootScopeProvider/this.$get</Scope.prototype.$evalAsync/<@http://localhost:8080/node_modules/angular/angular.js:16859:15
completeOutstandingRequest@http://localhost:8080/node_modules/angular/angular.js:5804:7
Browser/self.defer/timeoutId<@http://localhost:8080/node_modules/angular/angular.js:6081:7
我意识到此查询的变体已在论坛中浮出水面,但我无法将这些线程中提出的解决方案应用于我的代码。
答案 0 :(得分:1)
根据classifieds
变量(http://imgur.com/6d7fWga)的结果,我认为某些特定对象中缺少meta
个键。
请更新您的getTags
功能并告诉我您的输出:
function getTags(classifieds) {
var tags = [];
angular.forEach(classifieds, function(item) {
if(item.meta){
angular.forEach(item.meta.tags, function(tag) {
tags.push(tag);
});
}
});
return _.uniq(tags);
}
答案 1 :(得分:-1)
试试这个
angular.forEach(item.tags, function(tag) {
tags.push(tag);
});
而不是
angular.forEach(item.meta.tags,function(tag) {
tags.push(tag);
});