question

Ahmed Tayseer avatar image
Ahmed Tayseer asked

Cannot access the ItemInstanceId after executing GrantItemsToUsers

So after i grant a user an item from the catalogue am trying to add its ItemInstanceId to userdata but i get this error "Cannot read property 'ItemInstanceId' of undefined"

here is the code

var grantedItems = server.GrantItemsToUsers({ ItemGrants: [ { PlayFabId: currentPlayerId, ItemId: "AirStrike" }, { PlayFabId: currentPlayerId, ItemId: "Heal" } ] }); server.UpdateUserData("Loadout","{\"Abilities\":[\"" + grantedItems[0].ItemInstanceId + "\",\"" + grantedItems[1].ItemInstanceId + "\"]}");

CloudScript
10 |1200

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

Seth Du avatar image
Seth Du answered

The variable “grantedItems” in your code is the callback result of Grant Items To User API, which is not an array. Therefore, “grantedItems[0]” will be undefined, and “grantedItems[0].ItemInstanceId” will cause the error that you got.

You could try “grantedItems.ItemGrantResults[0].ItemInstanceId “ to get the data you want. Please refer to this API document Grant Items To User and check the correct format of the response.

In addition, you will still need to refer and define a proper request for UpdateUserData. you will need to specify PlayFabId and contain the information in the Data property of request. This API should be called sperately for each player.

10 |1200

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

Ahmed Tayseer avatar image
Ahmed Tayseer answered

Thanks for the anwer and for pointing out the other problems i had in the code, i actually looked at the documentation but it wasn't very clear for me, thanks again everything is working fine now.

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.