Does anybody have any Go server code they can share with me to show how to update the user’s metadata ? nk.AccountUpdateId seems to overwrite the metadata so I assume I have to read the existing metadata first and then add to it before writing it back with nk.AccountUpdateId ? I’m extremely new to GoLang so if someone’s already done this and could post their code then it would save me a lot of time. Thanks!
userId, ok := ctx.Value(runtime.RUNTIME_CTX_USER_ID).(string)
if !ok {
return "", runtime.NewError("could not get user ID from context", 13)
}
if err := nk.AccountUpdateId(ctx, userId, "", metadata, "", "", "", "", ""); err != nil {
return "", runtime.NewError("could not update account", 13)
}
Hello @Spuddy, you can modify the metadata as follows:
userId, ok := ctx.Value(runtime.RUNTIME_CTX_USER_ID).(string)
if !ok {
return "", runtime.NewError("could not get user ID from context", 13)
}
account, err := nk.AccountGetId(ctx, userId)
// err checking
var metadata map[string]any
err = json.Unmarshal([]byte(account.User.Metadata), metadata)
// err checking
// Apply modifications to metadata
metadata["foo"] = "bar"
if err := nk.AccountUpdateId(ctx, userId, "", metadata, "", "", "", "", ""); err != nil {
return "", runtime.NewError("could not update account", 13)
}
While the above works, mind that if you expect concurrent updates, it does not guarantee the metadata will be consistent, in which case you should use a storage object with conditional writes instead.
Thanks for the code. There shouldn’t be more than one device updating the same player’s metadata at the same time but do you think it’s okay to call the above code several times one after the other ? Do you know if the user’s metadata is updated on the server immediately before AccountUpdateId returns otherwise the second call to AccountGetId might return stale data ?
Sequential calls are fine, the issue may only occur if multiple calls are concurrently made within the same account. The server only updates the metadata when the respective runtime function is called.