question

sholzknecht975@gmail.com avatar image
sholzknecht975@gmail.com asked

Undefined Key

Trying to write server code to append to player data. I am having trouble knowing wither the entry is null/empty or not.

Current Function Code:

var playerEntryResult = server.GetUserData (
{
  PlayFabId: currentPlayerId,
  Keys: ["entry_"]
});

var newValue = "";
if (playerEntryResult["entry_"] === undefined || playerEntryResult["entry_"] === null)
  newValue += "test";
else
  newValue += playerEntryResult["entry_"] + "," + "test";

server.UpdateUserData({
PlayFabId: currentPlayerId,
Data: {
  entry_: newValue
}
});

10 |1200

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

brendan avatar image
brendan answered

The issue is that what is returned from the GetUserData call is a JSON object. If you use log to inspect the full details like this, you'll see what all is in it:

log.info(JSON.stringify(playerEntryResult));

It's a handy way to check what you get back from any of the server API calls in Cloud Script. So with that in mind, here's the script that'll do what you look like you have in mind above:

var playerEntryResult = server.GetUserData (
{
  PlayFabId: currentPlayerId,
  Keys: ["entry_"]
});

var newValue = "";
if (playerEntryResult.Data.entry_ === undefined || playerEntryResult.Data.entry_ === null)
  newValue += "test";
else
  newValue += playerEntryResult.Data.entry_.Value + "," + "test";

server.UpdateUserData({
  PlayFabId: currentPlayerId,
  Data: {
    entry_: newValue
  }
});

10 |1200

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

sholzknecht975@gmail.com avatar image
sholzknecht975@gmail.com answered

Alright, Thank you. This will work for now but, entry_ is just placeholder because after the underscore, there will be a date because I want to keep track of entries by days. For example, the key would be entry_052616 if they are adding an entry today. So is there a way to do this with a dynamic key name?

10 |1200

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

brendan avatar image
brendan answered
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.