Uncaught ReferenceError $ is not defined and Uncaught TypeError Cannot read property ‘SPGetCurrentUser’ of undefined

This tutorial, we will discuss how to fix an error that comes in SharePoint Online: $ in undefined jquery / Uncaught ReferenceError: $ is not defined or Uncaught TypeError: Cannot read property ‘SPGetCurrentUser’ of undefined, the error comes in the javascript object model.

Uncaught ReferenceError: $ is not defined

Recently I was working with the client object model in SharePoint online. In that demo, I was using the JavaScript object model (jsom) and also I was using jQuery also. I put the code inside a script editor web part inside the SharePoint Online web part page. But I got the error like below:

Uncaught ReferenceError: $ is not defined. It looks like below:

Uncaught ReferenceError jQuery is not defined
Uncaught ReferenceError jQuery is not defined

The problem was also coming because the jQuery was not loading properly on the page. To work with jQuery we need to refer jQuery.min.js file. Once I gave the references like below, the error did not come.

<script language="javascript" type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>

Uncaught TypeError: Cannot read property ‘SPGetCurrentUser’ of undefined

While working with SharePoint online client object model, I got the below error which says: Uncaught TypeError: Cannot read property ‘SPGetCurrentUser’ of undefined. It looks like below:

I was using SPServices to retrieve current user details like below:

var thisUser = $().SPServices.SPGetCurrentUser();
Cannot read property SPGetCurrentUser of undefined
Cannot read property SPGetCurrentUser of undefined

To work with SPServices, we need to refer both jquery.min.js and jquery.SPServices-2014.02.min.js files like below:

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery.SPServices/2014.02/jquery.SPServices-2014.02.min.js"></script>

Once you will refer the above .js files then the error will not come.

You may like following jQuery tutorials:

This tutorial, we learned how to fix error, Uncaught ReferenceError $ is not defined and Uncaught TypeError Cannot read property ‘SPGetCurrentUser’ of undefined

>