Solved

JSOM: The collection has not been initialized.

Posted on 2016-11-21
5
170 Views
Last Modified: 2017-01-31
I am using SharePoint 2010 and JSOM to get data from a list to populate a form that I have but I get the following error when I get to line 57.


The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.

// JavaScript source code
$(document).ready(function () {
    SP.SOD.executeOrDelayUntilScriptLoaded(retrieveData, "sp.js");
});

function retrieveData() {
    getListData("Project Module", "pm_OnQuerySucceeded", "queryError");
    getListData("Project Projections", "pp_OnQuerySucceeded", "queryError");
}


var getQueryString = function (field, url) {
    var href = url ? url : window.location.href;
    var reg = new RegExp('[?&]' + field + '=([^&#]*)', 'i');
    var string = reg.exec(href);
    return string ? string[1] : null;
};

function getListData(listName) {
    var clientContext = new SP.ClientContext();
    var oList = clientContext.get_web().get_lists().getByTitle(listName);

    if (getQueryString('projNumber')) {
        if (listName == "Project Module")
            var prjQry = "<View><Query><Where><Eq><FieldRef Name='Project_x0020_Number' /><Value Type='Text'>" + getQueryString('projNumber') + "</Value></Eq></Where></Query><ViewFields><FieldRef Name='Title' /><FieldRef Name='Project_x0020_Number' /><FieldRef Name='Station_x0020_Name' /><FieldRef Name='VISN' /><FieldRef Name='Station_x0020_Number' /><FieldRef Name='Total_x0020_Project_x0020_Cost' /></ViewFields></View>";
        else
            var prjQry = "<View><Query><Where><Eq><FieldRef Name='Project_x0020_Number' /><Value Type='Text'>" + getQueryString('projNumber') + "</Value></Eq></Where></Query><ViewFields><FieldRef Name='VISN' /></ViewFields></View>";
    }
    else {
        var prjQry = "<View><Query></Query></View>";
    }

    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml(prjQry);
    this.collListItem = oList.getItems(camlQuery);

    clientContext.load(collListItem);

    if (listName == "Project Module") {
        clientContext.executeQueryAsync(
	        Function.createDelegate(this, this.pm_OnQuerySucceeded),
	        Function.createDelegate(this, this.queryError)
	        );
    }
    else {
        clientContext.executeQueryAsync(
	        Function.createDelegate(this, this.pp_OnQuerySucceeded),
	        Function.createDelegate(this, this.queryError)
	        );

    }

}

function pm_OnQuerySucceeded(sender, args) {
    var listItemInfo = '';
    var listItemEnumerator = collListItem.getEnumerator();

    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();

        $('#visn').val(oListItem.get_item('VISN'));
        $("#projTitle").val(oListItem.get_item('Title'));
        $('#station').val(oListItem.get_item('Station_x0020_Number'));
        $('#stationName').val(oListItem.get_item('Station_x0020_Name'));
        $('#projNumber').val(oListItem.get_item('Project_x0020_Number'));
        $('#totProjCost').val(oListItem.get_item('Total_x0020_Project_x0020_Cost'));
    }

}

function pp_OnQuerySucceeded(sender, args) {
    var listItemInfo = '';
    var listItemEnumerator = collListItem.getEnumerator();

    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();

        $('#designPlan').val(oListItem.get_item('VISN'));
    }

}


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

Open in new window


Appreciate any help.

Thanks!
0
Comment
Question by:Isaac
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 41896887
Try changing line 35 from:
this.collListItem = oList.getItems(camlQuery);

Open in new window

to
collListItem = oList.getItems(camlQuery);

Open in new window

0
 
LVL 5

Author Comment

by:Isaac
ID: 41897334
I still get the same error.
0
 
LVL 5

Author Comment

by:Isaac
ID: 41897338
There must be some type of conflict b/c when I comment out line 8, my code works.
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 41898682
you've asynchronous operation here and your object collListItem is global
I mean the second getListData don't wait the first to end so you run twice collListItem.getEnumerator() before a new init
(like the previous question about get_item)

here we create two collListItem, collListem1 and collListem2
// JavaScript source code
$(document).ready(function () {
    SP.SOD.executeOrDelayUntilScriptLoaded(retrieveData, "sp.js");
});

function retrieveData() {

    getListData("Project Module", "pm_OnQuerySucceeded", "queryError");
    getListData("Project Projections", "pp_OnQuerySucceeded", "queryError");

    var clientContext = new SP.ClientContext();
    var oList = clientContext.get_web().get_lists().getByTitle(listName);

    if (getQueryString('projNumber')) {
        var prjQry = "<View><Query><Where><Eq><FieldRef Name='Project_x0020_Number' /><Value Type='Text'>" + getQueryString('projNumber') + "</Value></Eq></Where></Query><ViewFields><FieldRef Name='Title' /><FieldRef Name='Project_x0020_Number' /><FieldRef Name='Station_x0020_Name' /><FieldRef Name='VISN' /><FieldRef Name='Station_x0020_Number' /><FieldRef Name='Total_x0020_Project_x0020_Cost' /></ViewFields></View>";
    }
    else {
        var prjQry = "<View><Query></Query></View>";
    }

    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml(prjQry);

    this.collListItem1 = oList.getItems(camlQuery);

    clientContext.load(collListItem1);

    clientContext.executeQueryAsync(
        Function.createDelegate(this, this.pm_OnQuerySucceeded),
        Function.createDelegate(this, this.queryError)
    );


    // Project Projections

    if (getQueryString('projNumber')) {
        var prjQry = "<View><Query><Where><Eq><FieldRef Name='Project_x0020_Number' /><Value Type='Text'>" + getQueryString('projNumber') + "</Value></Eq></Where></Query><ViewFields><FieldRef Name='VISN' /></ViewFields></View>";
    }
    else {
        var prjQry = "<View><Query></Query></View>";
    }

    camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml(prjQry);

    this.collListItem2 = oList.getItems(camlQuery);

    clientContext.load(collListItem2);

    clientContext.executeQueryAsync(
        Function.createDelegate(this, this.pp_OnQuerySucceeded),
        Function.createDelegate(this, this.queryError)
    );
}


var getQueryString = function (field, url) {
    var href = url ? url : window.location.href;
    var reg = new RegExp('[?&]' + field + '=([^&#]*)', 'i');
    var string = reg.exec(href);
    return string ? string[1] : null;
};


function pm_OnQuerySucceeded(sender, args) {
    var listItemInfo = '';
    var listItemEnumerator = collListItem1.getEnumerator();

    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();

        $('#visn').val(oListItem.get_item('VISN'));
        $("#projTitle").val(oListItem.get_item('Title'));
        $('#station').val(oListItem.get_item('Station_x0020_Number'));
        $('#stationName').val(oListItem.get_item('Station_x0020_Name'));
        $('#projNumber').val(oListItem.get_item('Project_x0020_Number'));
        $('#totProjCost').val(oListItem.get_item('Total_x0020_Project_x0020_Cost'));
    }

}

function pp_OnQuerySucceeded(sender, args) {
    var listItemInfo = '';
    var listItemEnumerator = collListItem2.getEnumerator();

    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();

        $('#designPlan').val(oListItem.get_item('VISN'));
    }

}


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

Open in new window

0
 
LVL 5

Author Closing Comment

by:Isaac
ID: 41898785
Thanks!
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question