I am trying to take selected listed items and pack specific data values from them into either an array or JSON "object" to then stringify and store in local browser session storage so I can pass it to a page for making labels for the selected list items.
Here's some of the code
function parseSelectedItemsForLabels ()
{
if (selectedItems.length > 0)
{
var labelDataTemp = {
sampleNumber:"blank",
sampleModel:"blank",
sampleRev:"blank",
sampleDate:"blank",
sampleNumLabels: 0
};
var labelDataObject = {};
var id;
var dateSubstring;
var targetList = pnp.sp.web.lists.getById("827E50EB-2EE9-43F0-B354-542BDB79C8AE");
selectedItems.forEach
(
function(item)
{
id = item.id;
targetList.items.getById(id).get().then
(
(item) =>
{
dateSubstring = item.Received.substring(0,10);
labelDataTemp =
{
"sampleNumber": item.Sample_x0020__x0023_.toString(),
"sampleModel": item.Model_x0020__x0023_.toString(),
"sampleRev": item.Rev.toString(),
"sampleDate": dateSubstring.toString(),
"sampleNumLabels": 0
};
var nextKey = Object.keys(labelDataObject).length+1;
labelDataObject[nextKey] = labelDataTemp;
}
);
}
);
}
openLabelMakerForm(labelDataObject);
}
function openLabelMakerForm (labelData)
{
console.log("Test Statically Typed JSON Recursion");
var stringifyTestObj = {
1: {sampleNumber: "A02812", sampleModel: "JWM40", sampleRev: "H", sampleDate: "2019-01-29", sampleNumLabels: 0},
2: {sampleNumber: "A02810", sampleModel: "WVOM43", sampleRev: "D", sampleDate: "2019-01-28", sampleNumLabels: 0},
3: {sampleNumber: "A02811", sampleModel: "WVOM43", sampleRev: "D", sampleDate: "2019-01-28", sampleNumLabels: 0},
4: {sampleNumber: "A02814", sampleModel: "JWM40", sampleRev: "H", sampleDate: "2019-01-29", sampleNumLabels: 0},
5: {sampleNumber: "A02813", sampleModel: "JWM40", sampleRev: "H", sampleDate: "2019-01-29", sampleNumLabels: 0}
}
console.log(stringifyTestObj);
var stringifyTest = JSON.stringify(stringifyTestObj);
console.log(stringifyTest);
var options = {
title: "Sample Labels - Set Model Qty",
url: "/Lists/Engineering Sample Database/SampleLabels.aspx"
}
//sessionStorage.setItem("labelData", JSON.stringify(labelData));
console.log(JSON.parse(stringifyTest));
console.log("----- END TEST -----");
console.log("Compare Test Object Above to Dynamic Object Below");
console.log(labelData);
console.log(JSON.stringify(labelData));
//console.log(JSON.parse(sessionStorage.getItem("labelData")));
SP.UI.ModalDialog.showModalDialog(options);
}
Here's the console output I get.
As you can see, the dynamically constructed JSON object shows up different in the console and will not stringify. However, the statically typed "hand made" JSON object using the same structure and keys and similar values does work properly.
I've tried using an array of objects [] and that has the same issue.
Am I constructing my dynamic object wrong? I've tried building it with strings, pushing to an array, and several other things. I have the same issue with the dynamic object every time. Keep in mind that I'm building an object or array of other objects here.