question

Kim Strasser avatar image
Kim Strasser asked

CloudScript: Is it possible to find an item in the Catalog when I use the displayname to find it?

I want to find an item in the Catalog but I'm not sure if it is possible to find an item when I use the displayname to find it.

I get this error message:

  "Error": {
            "Error": "JavascriptException",
            "Message": "JavascriptException",
            "StackTrace": "SyntaxError: Unexpected token L in JSON at position 0\n    at JSON.parse (<anonymous>)\n    at E5E2C-main.js:415:23\n    at Array.find (<anonymous>)\n    at handlers.GrantingLeaderboardRewardsToPlayer (E5E2C-main.js:413:34)\n    at Object.invokeFunction (Script:117:33)"
        }
var displaynameenglish = "Bamboo Sword";
var request = {
        CatalogVersion: "MyShop"
    };
  
var result = server.GetCatalogItems(request);
var itemObj = result.Catalog.find(function (obj)
{
    return JSON.parse(obj.DisplayName).English === displaynameenglish
});  // find the item in the catalog

var tags = itemObj.Tags;

Is it possible to use the English displayname of the item when I use result.Catalog.find in CloudScript?

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.

1 Answer

·
Seth Du avatar image
Seth Du answered

To be clear, PlayFab doesn’t have an API that can retrieve the information of a single item in a catalog. There is only GetCatalogItems, GetStoreItems or GetUserInventory, which will get the full content of inventory or catalog.

The common scenario is to get a full list of items and cache locally as an object or any data format you have defined, then search through C# language (or any other you use).

I am not sure what your code indicates, but here is my solution to your requirement.

handlers.test = function (args, context) {
    var displaynameenglish = "xxx"; 
    var request = {
        CatalogVersion: "xxxxxxxxx"
    }; 
    var result = server.GetCatalogItems(request); 
    var itemObj = result.Catalog.find(item => item.ItemId == displaynameenglish); 
    return JSON.parse(itemObj.DisplayName).English;
}
2 comments
10 |1200

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

Kim Strasser avatar image Kim Strasser commented ·

It works now. But I needed to change the Item ID in my Catalog to the English display name of the item.

0 Likes 0 ·
Seth Du avatar image Seth Du ♦ Kim Strasser commented ·

It is only available in Admin API, which is not supported by Cloud Script.

0 Likes 0 ·

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.