question

brendan avatar image
brendan asked

Cloud Script - how do I write data to a Key which is defined dynamically

Brendan Vanous
started a topic on Thu, 14 May 2015 at 2:06 PM

Question from a developer:

I need to write to a Key which is defined in a string, as part of a Cloud Script call. But each time I try this, I get the variable name as the Key. What's going on here, and how do I get this to work?

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

·
brendan avatar image
brendan answered

Best Answer
Brendan Vanous said on Thu, 14 May 2015 at 2:07 PM

Basically, JSON Keys are treated as strings, whether they're quoted or not. So if you have:

    var keyString = "foo";
    server.UpdateUserData({
        PlayFabId: currentPlayerId,
        Data: { keyString : keyString }
    });

You'd get the Key/Value pair { "keyString" : "foo" }. Really irritating, right? Fortunately, there's a workaround. If you define it as an object like this, it works:

    var keyString = "foo";
    var dataPayload = {};
    dataPayload[keyString] = "bar";
    server.UpdateUserData({
        PlayFabId: currentPlayerId,
        Data: dataPayload
    });

This results in the Key/Value pair { "foo" : "bar" }, which is what you're gong for. Note that you can't initialize it with the values, though. Defining dataPayload like this wouldn't work:

    var keyString = "foo";
    var dataPayload = { keyString, "bar" };
    server.UpdateUserData({
        PlayFabId: currentPlayerId,
        Data: dataPayload
    });

Yep, you guessed it - the keyString is now a Key, making the Key/Value pair { "keyString", "bar" } - not what you want.


1 Comment
Brendan Vanous said on Thu, 14 May 2015 at 2:07 PM

Basically, JSON Keys are treated as strings, whether they're quoted or not. So if you have:

    var keyString = "foo";
    server.UpdateUserData({
        PlayFabId: currentPlayerId,
        Data: { keyString : keyString }
    });

You'd get the Key/Value pair { "keyString" : "foo" }. Really irritating, right? Fortunately, there's a workaround. If you define it as an object like this, it works:

    var keyString = "foo";
    var dataPayload = {};
    dataPayload[keyString] = "bar";
    server.UpdateUserData({
        PlayFabId: currentPlayerId,
        Data: dataPayload
    });

This results in the Key/Value pair { "foo" : "bar" }, which is what you're gong for. Note that you can't initialize it with the values, though. Defining dataPayload like this wouldn't work:

    var keyString = "foo";
    var dataPayload = { keyString, "bar" };
    server.UpdateUserData({
        PlayFabId: currentPlayerId,
        Data: dataPayload
    });

Yep, you guessed it - the keyString is now a Key, making the Key/Value pair { "keyString", "bar" } - not what you want.

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.