Solved

JSOM: The collection has not been initialized.

Posted on 2016-11-21
5
89 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
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

789 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