0

I'm calling a function to enumerate through a list of items with a particular ID\foreign key. When there are no items listed I get the appropriate response back, however, if any records are available I get those records back in the response with "undefined" in front of it. See below.

Ancillary Equipment: undefinedSFP RJ-45 (Cisco GLC-T): 1

Any way to remove or filter out "undefined"?

// GET ASSOCIATED ANCILLARY EQUIPMENT FOR SITE RECORDS
function getAncillary() {
dfd = $.Deferred();
    var id, strMessage2, str, strFinal;
    parentId = GetUrlKeyValue("ID", false, location.href); 
    var context = SP.ClientContext.get_current();
    var web = context.get_web();
    context.load(web);
    var list = web.get_lists().getByTitle("Ancillary 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, Equipment_x0020_Count, Ancillary_x0020_Equipment)");
    context.executeQueryAsync(function() {
        var count = listItems.get_count();
        var listEnumerator = listItems.getEnumerator();
        while (listEnumerator.moveNext()) {
            listItem = listEnumerator.get_current();
            id = listItem.get_item("ID");
            varCount = listItem.get_item('Equipment_x0020_Count');
            varEquip = listItem.get_item('Ancillary_x0020_Equipment');
            strMessage2 = varEquip + ": " + varCount + " \n";
            str += strMessage2;
        } 
        if (count > 0) {
            strFinal = "Ancillary Equipment: \n" + str;
        } else {
            strFinal = "No ancillary equipment...";
        }
        dfd.resolve(strFinal);
    }, function() {
        dfd.reject();
    });
    return dfd.promise();
}
2
  • 1
    Try initializing str, strMessage2, strFinal to blank string line str="" . Commented May 1, 2019 at 13:57
  • Welcome. Please upvote and accept the below answer. Commented May 1, 2019 at 19:07

1 Answer 1

1

I can see there is some error in your code.

Try initializing the variables you are using like str, strMessage2, strFinal to blank string.

For Example:

var str="";

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.