Display_Here
Display_Here

Reputation: 311

Error in javascript file but not console?

The error in question

Uncaught Error: The property or field has not been initialized. 
It has not been requested or the request has not been executed. 
It may need to be explicitly requested. 

Here is the code, just trying to get a simple count of a list.

var CustomAction = function(){

    var clientContext = new SP.ClientContext.get_current();
    var web = clientContext.get_web(); 
    this.oList = web.get_lists().getByTitle("Classification");

    // .load() tells CSOM to load the properties of this object
    // multiple .load()s can be stacked
    clientContext.load(oList);

    // now start the asynchronous call and perform all commands
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onSuccess), Function.createDelegate(this, this.onFailure));

    // method will exit here and onSuccess or OnFail will be called asynchronously

};

function onSuccess(sender, args) {
    alert('No of rows: ' + oList.get_itemCount());
};

function onFail(sender, args) {
    alert('Request failed.\n' + args.get_message() + '\n' + args.get_stackTrace());
};

The error occurs at oList.get_itemCount(). What reason would there be for this to be happening? I tried using $( document).ready and $(window).onload but the problem still occurs. So like I said, it works when I copy/paste that into the browser but running it from file it doesn't.

Upvotes: 1

Views: 392

Answers (1)

kei
kei

Reputation: 20481

Try this instead

var CustomAction = function(){

    var clientContext = new SP.ClientContext.get_current();
    var web = clientContext.get_web(); 
    this.oList = web.get_lists().getByTitle("Classification");

    // .load() tells CSOM to load the properties of this object
    // multiple .load()s can be stacked
    clientContext.load(oList);

    // now start the asynchronous call and perform all commands

    clientContext.executeQueryAsync((function(sender, args){alert('No of rows: ' + oList.get_itemCount())}(this, this.onSuccess)), (function(sender, args){alert('Request failed.\n' + args.get_message() + '\n' + args.get_stackTrace())}(this, this.onFailure)));
};

Upvotes: 1

Related Questions