Chapter 10 Type Error calling Submit pg 219

formhandler.js:27 Uncaught TypeError: Cannot set property ‘coffee’ of undefined
at formhandler.js:27
at Array.forEach ()
at HTMLFormElement. (formhandler.js:25)
at HTMLFormElement.dispatch (jquery.js:4435)
at HTMLFormElement.r.handle (jquery.js:4121)

after
var fh = new App.FormHandler(’[data-coffee-order=“form”]’);

undefined

fh.addSubmitHandler();

Making a second pass through the book te freshen up on some of the concepts. ithought there was an errate on this page but i cant find it.

Can you post your code for formhandler.js?

(function (window) {

"use strict";

var App = window.App || {};

var $ = window.jQuery;

function FormHandler(selector) {

    if(!selector)

    {

        throw new Error('No Selector Provided');

    }

    this.$formElement = $(selector);

    if(this.$formElement.length==0)

    {

        throw new Error('Could not find element with selector: ' + selector);

    }

  };

  FormHandler.prototype.addSubmitHandler = function(fn){

      console.log('Setting Submit handler for form');

      this.$formElement.on('submit', function (event){

            event.preventDefault();

            //var data = $(this).serializeArray();

            var data = $(this).serializeArray().forEach(

                    function (item){

                        data[item.name] = item.value;

                        console.log(item.name + ' is ' + item.value);

                   });

            console.log(data);

    });

  }

App.FormHandler = FormHandler;

window.App = App;

})(window);

Thank you.
I found my mistake.
var data = {};

             $(this).serializeArray().forEach(

I wasnt intializing the data array.

1 Like