angular.js:14110 TypeError: Cannot read property 'push' of undefined

angular.js:14110 TypeError: Cannot read property 'push' of undefined

我试图在每个日记对象上创建一个数组,并将与日记 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

您需要检查 diary.foods 实际上是一个数组。修改日记没问题的话试试

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