0

I'm looping through records and setting a variable according to an if statement. I'm trying to build the output variable with the results of all the looped records into one string using +=. I'm getting the syntax error referenced in the title of this thread. Not sure how to fix this or if I'm even using the right statement. See code below.

 function getEquipment() {
        dfd = $.Deferred();
            var id, hardWare, currentQty, newQty, minusCalc, strMessage;
            var list = web.get_lists().getByTitle("SD-WAN Equipment");
            var caml = new SP.CamlQuery();
            caml.set_viewXml('<View><Query><Where><Eq><FieldRef Name="LinkID" /><Value Type="Lookup">' + parentId + '</Value></Eq></Where></Query></View>');
            listItems = list.getItems(caml);
            context.load(listItems, "Include(ID, Hardware_x0020_Model, New, Current)");
            context.executeQueryAsync(function() {
                var count = listItems.get_count();
                var listEnumerator = listItems.getEnumerator();
                while (listEnumerator.moveNext()) {
                    listItem = listEnumerator.get_current();
                    id = listItem.get_item("ID");
                    hardWare = listItem.get_item('Hardware_x0020_Model');
                    newQty = listItem.get_item('New');
                    currentQty = listItem.get_item('Current');
                    minusCalc = newQty - currentQty;
                    var diff = function (newQty, currentQty) { return Math.abs(newQty - currentQty);}
                    if (minusCalc > 0) {
                        strMessage = "Shipped: " + hardWare + " (" + diff(newQty,currentQty) +") \n";
                    } else if (minusCalc < 0) {
                        strMessage = "Reclaimed: " + hardWare + " (" + diff(newQty,currentQty) +") \n";
                    }
                    var strFinal += strMessage;  <==== ERROR HERE
                } // End while
                dfd.resolve();
            }, function() {
                dfd.reject();
            });
            return dfd.promise();
    } //End getEquipment
2

If you want to accumulate the results of your loop, you should declare a variable outside of it and then use inside the loop:

 var strFinal = '';
 while (listEnumerator.moveNext()) {
    ....
    strFinal += strMessage;
} // End while
  • This is the answer, you can't attach some value to a newly created variable – Marco Dec 12 '18 at 18:21
1

It looks like you're re-declaring the variable. I can't recall with Javascript but normally you can't use the += assignment statement when also declaring the variable. Try this for your onSuccess function:

function() {
    var count = listItems.get_count();
    var listEnumerator = listItems.getEnumerator();
    var strFinal='';
    while ( listEnumerator.moveNext() ) {
        listItem = listEnumerator.get_current();
        id = listItem.get_item( "ID" );
        hardWare = listItem.get_item( 'Hardware_x0020_Model' );
        newQty = listItem.get_item( 'New' );
        currentQty = listItem.get_item( 'Current' );
        minusCalc = newQty - currentQty;
        var diff = function( newQty, currentQty ) {
            return Math.abs( newQty - currentQty );
        }
        if ( minusCalc > 0 ) {
            strMessage = "Shipped: " + hardWare + " (" + diff( newQty, currentQty ) + ") \n";
        } else if ( minusCalc < 0 ) {
            strMessage = "Reclaimed: " + hardWare + " (" + diff( newQty, currentQty ) + ") \n";
        }
        strFinal += strMessage; // <= === ERROR HERE
    } // End while
    dfd.resolve();
}

Where you declare the var strFinal; outside the While loop, rather than declaring it with the assignment operator like you had (ie var strFinal = strMessage)

0

Why syntax error? Because you try to concatenate value to undeclared variable.

Try this is your browser:

var a = 'abc';
var b += a;

It will give you same error.

Your variable strFinal not declared before += operator. += require to variable be declared before operation.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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