My CloudScript function is below.
handlers.setTitleDataCloudScript = function (args, context) { var collectionName = args.collection; var itemKey = args.itemKey; var itemValue = args.itemValue; log.debug("setTitleDataCloudScript: args=", JSON.stringify(args)); log.debug("setTitleDataCloudScript: context=", JSON.stringify(context)); //get title data var getTitleDataRequest = { "Keys": [collectionName] }; var getTitleDataResponse = server.GetTitleData(getTitleDataRequest); if (!getTitleDataResponse.Data.hasOwnProperty(collectionName)) { log.error("Collection not found in TitleData. Exiting..."); return JSON.stringify([]); } else { log.debug("setTitleDataCloudScript: getTitleDataResponse=", JSON.stringify(getTitleDataResponse)); var collection = getTitleDataResponse.Data.collection; log.debug("setTitleDataCloudScript: collection=", JSON.stringify(collection)); collection[itemKey] = itemValue; if (!collection.hasOwnProperty(itemKey)) { log.error("Item could not found in Collection. Exiting..."); return JSON.stringify([]); } else { var setTitleDataRequest = { "Key": collectionName, "Value": collection }; var setTitleDataResponse = server.SetTitleData(setTitleDataRequest); return itemValue; } } };
The hope with this function is that when passed something like args="{\"collection\":\"Products\",\"itemKey\":\"Mountain Dew\",\"itemValue\":{\"Line\":\"grocery\",\"category\":\"soda\"}}" that it will find the Products object in my project's TitleData and add to it the item Mountain Dew (or update Mountain Dew if it is already present. However, the line
collection[itemKey] = itemValue;
always fails with the error
"StackTrace": "TypeError: Cannot set property 'Mountain Dew' of undefined\n at handlers.setTitleDataCloudScript (C3CB-main.js:421:29)"
Since it doesn't seem like the JS for CloudScripts includes the Object.assign() function, I know of no other way to add another item to the object collection looked up - "Products" in this case.
Please note, I have altered the object data for this publicly posted question. The data in our actual C3CB project is different.
Any assistance you can offer on this would be greatly appreciated.
Thanks.