question

Kim Strasser avatar image
Kim Strasser asked

How can I get an item's CustomData in CloudScript?

How can I get an item's CustomData in CloudScript?

I get an error message when I try to get the CustomData keys and values.

"Error": {
            "Message": "JavascriptException",
            "Error": "JavascriptException",
            "StackTrace": "TypeError: itemObj.CustomData.forEach is not a function\n    at handlers.GrantingLeaderboardRewardsToPlayer (E5E2C-main.js:328:25)\n    at Object.invokeFunction (Script:117:33)"
        },
var itemId = "ItemBambooSword";
var request = {
        CatalogVersion: "MyShop"
    	};
  
var result = server.GetCatalogItems(request);
var itemObj = result.Catalog.find(function (obj)
{
    return obj.ItemId === itemId
});
  
var customdatakeylist = [];
var customdatavaluewoodlist = [];
var customdatavalueironlist = [];
var customdatavaluediamondlist = [];

itemObj.CustomData.forEach(element => {
         customdatakeylist.push(element.Key);
         customdatavaluewoodlist.push(element.Value.Wood);
         customdatavalueironlist.push(element.Value.Iron);
         customdatavaluediamondlist.push(element.Value.Diamond);
        });

CloudScript
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Ivan Cai avatar image Ivan Cai ♦ commented ·

"ItemObj.CustomData" is of string type and cannot be traversed by foreach, so an error will occur. You can try to convert it from string to object type.

0 Likes 0 ·

1 Answer

·
Kim Strasser avatar image
Kim Strasser answered

Thanx. It works now.

var myObject = JSON.parse(itemObj.CustomData);
var jsonitemskeys = Object.keys(myObject);

jsonitemskeys.forEach(function(entry) {
log.info("Key: " + entry);
log.info("Value: " + myObject[entry]);

var myObject1 = JSON.parse(myObject[entry]);
log.info("ValueWood: " + myObject1["Wood"]);
log.info("ValueIron: " + myObject1["Iron"]);
log.info("ValueDiamond: " + myObject1["Diamond"]);
    });
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.