angular.js:14110 TypeError:无法读取undefined的属性'push'

时间:2016-12-14 15:49:41

标签: javascript angularjs firebase factory-method

我正在尝试在每个日记对象上创建一个数组,并将与日记id匹配的食物推送到该数组。有人看到我能做到这一点吗?或者原因不是吗?

(Diary Controller)
app.controller("DiaryCtrl", function($scope, $rootScope, $location, DiaryFactory, FoodFactory){

$scope.diaries = [];
$scope.foods = [];


//getMeals
//lists all meals on the diary page
let getAllDiaries = function(){
    DiaryFactory.getDiary($rootScope.user.uid).then(function(FbDiaries) {
        console.log('diaries: ', FbDiaries);
        FoodFactory.getFoodsFB($rootScope.user.uid).then(function(FbFoods){
            console.log('foods from controller', FbFoods);
            FbFoods.forEach(function(food){
                FbDiaries.forEach(function(diary){
                    console.log('foods', food);
                    if(food.mealId === diary.id){
                        diary.foods.push(food);
                        console.log('foods array on diary', diary.foods);
                    }
                });
            });
        });
    });
};
getAllDiaries();

Console error message

1 个答案:

答案 0 :(得分:0)

你需要检查日记。食物实际上是一个数组。如果可以修改日记,请尝试

diary.foods = diary.foods || [];
diary.foods.push(food);