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();
}
str="".