Cannot read property 'post' of undefined

I’m on Page 263 . Getting the
Cannot read property ‘post’ of undefined when I call the code from the

var RemoteDS = new App.RemoteDataStore(“http://coffeerun-v2-rest-api.herokuapp.com/api/coffeeorders”);
RemoteDS.add(‘dr@who.com’,{emailAddress:‘dr@who.com’ ,coffee:‘espresso’});

remotedatastore.js
(
function(window) {
‘use strict’;
var App = window.App || {};
var $ = window.jQuery;

    function RemoteDataStore(url) {
        if (!url) {
            throw new Error('No Remote URL Supplied');
        }

        this.serverUrl = url;
    }

    RemoteDataStore.prototype.add = function(key, val) {
        $.post(this.serverUrl, val, function(serverResponse) {
            console.log(serverResponse);
        });
    };

    App.RemoteDataStore = RemoteDataStore;
    window.App = App;
})(window);

any ideas where to look for the fix ?

I got the console test to run by commenting //var $ = window.jQuery;
I used the solution files which are a little further along but do not contain that declaration.
The test ran. very cool. need to better understand why this line is the issue. I believe it may have been my challenge in an earlier exercise. Does it become unnecessary at some point in the project?

Hi @ddecasse! Hmm, if you’re commenting out var $ = window.jQuery; that’s sidestepping the intention behind parameter mapping (e.g. injecting the window object to the IIFE).

Could you try uncommenting out that line and we can walk through to see what’s happening? Perhaps put a breakpoint at the var $ = window.jQuery; line and see what’s going on?

I’ll bet your <script> tag for jQuery comes later than it should in index.html—make sure it comes before the others so the jQuery variable is defined before your own code runs :slight_smile:

I will give that a try. Any idea why the solution files wouldnt have had it?

@nybblr Thank you. I uncommented the line and moved the jquery script tag to the top of the list the code runs fine. Now the error makes sense.

@ddecasse Awesome! Do the solutions make sense now or do they still look funky?

as far as I remember the solution didnt have the line that i commented out.

Just wanted to post that I ran into this error and it was that my cdn jquery reference was using a slim version and my guess (I’ll research this more) is that this version of jquery does not implement the .post method.

thanks for the follow up. I am on the last project now. I must say i love
the book . but issues like this have detracted a little bit. i may be doing
hte labs all over again just to get the best idea of all of it. maybe.

Daniel, I hear you!

I’m working thru all the chapters on JS in preparation for teaching JS in the Fall semester. I’ve found a couple of things that did not seem to work, upon review of the instructions and my code, 100% of the time I’ve found that I missed something.

This can also help us work on our debugging skill-set, which will serve us well as we continue our programming adventures.

Keep up the great work!
Rio :slight_smile: