简体   繁体   English

angular.js:13920 TypeError:无法读取null的属性'push'

[英]angular.js:13920 TypeError: Cannot read property 'push' of null

This program is meant to send data in JSON from factory which will be used later, to localhost but instead i get the error. 该程序旨在将JSON格式的数据从工厂发送到localhost,但稍后会出现错误。 When one selects an order the order is sent to another orders. 当一个人选择一个订单时,该订单将被发送到另一个订单。 When i click select button is when the error occurs in the console. 当我单击选择按钮时,就是控制台中发生错误。

Main html page html主页

<div class="jumbotron">
  <h3>'Allo, 'Allo!</h3>
  <p class="lead">
    <img src="images/yeoman.png" alt="I'm Yeoman"><br>
   Welcome to AMPATH Dev Meal System.
  </p>
  <p><a class="btn btn-lg btn-success" ng-href="#/">Splendid!<span class="glyphicon glyphicon-ok"></span></a></p>
</div>

<div class="row marketing">

    <h3>Select The Restaurant of your Choice</h3>
        <!--<form name="menuForm"-->
            <!--Displaying restnames in lists-->
<form>
    <h2>Restaurants</h2>
    <ul> <h3>
        <li ng-repeat="(key,Cafeteria) in myRestaurant">
            {{Cafeteria.restname}} 
        <div>
        <button class="btn btn-lg btn-success" ng-submit="order" ng-click="orderfood(key,Cafeteria)">Check foods</button> 
        </div>
        </li>
        </h3>
    </ul>
</form>
</div>
<div>
 <table border="1" class="w3-table w3-striped w3-bordered w3-border">
     {{me}}
        <thead>
            <tr>
                <td>Food Name</td>
                <td>Price</td>
                <td>Select your order</td>
            </tr>
        </thead>

        <tbody>
            <tr ng-repeat="food in foods">
                <td>{{food.foodname}}</td>
                <td>{{food.price | currency}}</td>
                <td><button class="w3-btn w3-ripple" value="Show alert" ng-click="storage(food)">&#4598; Select</button></td>
            </tr>
        </tbody>
    </table>
</div>

and the controller meant to sent list from factory to the local storage.. 控制器打算将清单从工厂发送到本地存储。

'use strict'; “严格使用”;

/**
 * @ngdoc function
 * @name mealsApp.controller:AboutCtrl
 * @description
 * # AboutCtrl
 * Controller of the mealsApp
 */
angular.module('mealsApp')
  .controller('MenuCtrl', function ($scope, menu, $window) {
    this.awesomeThings = [
      'HTML5 Boilerplate',
      'AngularJS',
      'Karma'
    ];
    // $scope.foods;

    // reading data from localstorage
    var orderedfood = JSON.parse($window.localStorage.getItem("orderedfoods"));
    //console.log(orderedfood);

    // Defining restaurant and ordered food
    $scope.myRestaurant = menu.myRestaurant;
    $scope.orderfood = function(key,Cafeteria){
      // console.log(key);

      // Defining foods in a restaurants 
      $scope.foods = Cafeteria.foods;
      // console.log(Cafeteria.foods);

      // Getting foods to local storage
      $scope.storage = function (food){

        orderedfood.push({
          foodname : food.foodname,
          price : food.price,
          orderedfood : true,
          deliveredfood : false
        });

        // Sending data to localstorage
        $window.localStorage.setItem("orderedfoods", JSON.stringify(orderedfood));
      };
      /*$scope.order = function(){
        //$window.alert("You selected" + foodname);
      };*/

    };

  });

I would appreciate your answer 谢谢您的回答

It seems that what is returned by JSON.parse($window.localStorage.getItem("orderedfoods")) is not an array. 似乎JSON.parse($window.localStorage.getItem("orderedfoods"))的不是数组。 maybe there is nothing in localstorage? 也许本地存储中没有任何东西? When you address a nonexistant item in localstorage it returns null . 当您处理localstorage中不存在的项目时,它将返回null You can try to fix it like this: 您可以尝试像这样修复它:

var orderedfood = JSON.parse($window.localStorage.getItem("orderedfoods"))||[];

Your orderedfood array object is null. 您的orderedfood数组对象为空。 You can uncomment the console log and confirm that. 您可以取消注释控制台日志并确认。 To avoid that error you may want to check for null and assign an empty array to the orderedfood , like following: 为了避免该错误,您可能需要检查null并将空数组分配给orderedfood ,如下所示:

 var orderedfood = JSON.parse($window.localStorage.getItem("orderedfoods"));
 console.log(orderedfood);
orderedfood = orderedfood || [];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 angular.js:14110 TypeError:无法读取undefined的属性&#39;push&#39; - angular.js:14110 TypeError: Cannot read property 'push' of undefined 3angular.js:13920 TypeError:无法设置未定义的属性&#39;contract&#39; - 3angular.js:13920 TypeError: Cannot set property 'contract' of undefined Angular.JS Uncaught TypeError:无法分配给只读属性 - Angular.JS Uncaught TypeError: Cannot assign to read only property Angular.js:10671 TypeError:无法读取未定义的属性 - Angular.js:10671 TypeError: Cannot read property of undefined angular.js:12520 TypeError:无法读取未定义的属性&#39;post&#39; - angular.js:12520 TypeError: Cannot read property 'post' of undefined angular.js:13550 TypeError:无法读取未定义的属性“编译”? - angular.js:13550 TypeError: Cannot read property 'compile' of undefined? 类型错误:无法在反应 js 中读取 null 的属性“推送” - TypeError: Cannot read property 'push' of null in react js 错误TypeError:无法在Angular 6中读取null的属性&#39;push&#39; - ERROR TypeError: Cannot read property 'push' of null in Angular 6 TypeError:无法在addItemToCart读取null的属性“ push” - TypeError: Cannot read property 'push' of null at addItemToCart 错误类型错误:无法读取 null 的属性“推送” - ERROR TypeError: Cannot read property 'push' of null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM