0

I want to return each element of an array object to the console. The code below does return the object, but not sequentially as I would like. When I select Daffy Duck and Bugs Bunny from a view as selected items the array will return "undefinedDaffy DuckBugs Bunny" after the executeQuery fire off.

I'm not sure where the undefined is coming from. I of course want to return Daffy and Bugs individually to the console, however, after the promise resolves I get the following:

undefinedDaffy DuckBugs Bunny
undefined

function callAccepted() {
    getSelected().done(function(objects, iLength) { 
    // Stuff
        for (var k = 0; k < iLength; k++) {
            console.log(objects[k]);
        }
    });
} // End callAccepted



// Get user information function
function getSelected() {
    var dfd = $.Deferred(function(){
        var ctx = SP.ClientContext.get_current();
        var clientContext = new SP.ClientContext(); 
        var targetList = clientContext.get_web().get_lists().getByTitle(ListName);
        var SelectedItems = SP.ListOperation.Selection.getSelectedItems(ctx);
        var items = [];
        for (var i in SelectedItems) {
            var id = SelectedItems[i].id;
            var item = targetList.getItemById(id);
            clientContext.load(item, "Title");
            items.push(item);
        } 
        clientContext.executeQueryAsync(
            function(){ 
                for (var j = 0; j < items.length; j++) {
                    var arrItems = [];
                    var itemObjects;
                    var itemLength = items.length;
                    itemObjects += items[j].get_item("Title");
                    arrItems.push(itemObjects);
                }
                dfd.resolve(arrItems, itemLength);
            },
            function(){ 
                dfd.reject(args.get_message());
            }
        );
    }); // End dfd
  return dfd.promise();
} // End getSelected
2
  • 1
    The example you refer to only shows one Context and does not do loops inside of loops. You introduce more synchronous loops and a second Context and then do asynchronous operations.. which ofcourse f*s up your synchronous loop, because unless you go for ES6 generator functions Loops can't deal with async code. So take 2 steps back, don't copy/paste examples unless you fully understand why they work, First learn JSOM & Async patterns with Promises, Recursion or whatever suits your need. Apr 25, 2017 at 14:07
  • 1
    As Danny stated instead of doing a for loop inside a success function and then calling another jsom context from scratch inside of it, try using jquery promises to gather all the items you need, then return a promise and iterate through the array to write afterwards not during.
    – Mike
    Apr 25, 2017 at 19:00

1 Answer 1

0

Working code:

// Grabs selected items from getSelected function and passes parameters to writeSelected function
function callAccepted() {
    getSelected().done(function(varObjects, iLength) { 
        for (var k in varObjects) {
            console.log(varObjects[k]);
        }

    }); // End getSelected
} // End callAccepted

// Grabs selected items, accepts input from callAccepted or callRejected functions
function getSelected() {
    var dfd = $.Deferred(function(){
        var ctx = SP.ClientContext.get_current();
        var clientContext = new SP.ClientContext(); 
        var targetList = clientContext.get_web().get_lists().getByTitle(ListName);
        var SelectedItems = SP.ListOperation.Selection.getSelectedItems(ctx);
        var items = [];
        var arrItems = [];
        for (var i in SelectedItems) {
            var id = SelectedItems[i].id;
            var item = targetList.getItemById(id);
            clientContext.load(item, "Title");
            items.push(item);
        } // End for
        clientContext.executeQueryAsync(
            function(){ // Return to button click function
                var itemLength = 0;
                var itemObjects = [];
                for (var j = 0; j < items.length; j++) {
                    itemObjects = items[j].get_item("Title");
                    itemLength += itemObjects;
                    arrItems.push(itemObjects);
                }
                dfd.resolve(arrItems, itemLength);
            },
            function(){ // Return to button click function
                dfd.reject(args.get_message());
            }
        ); // End ClientContext
    }); // End dfd
  return dfd.promise();
} // End getSelected

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.