【问题标题】:Google Firestore Setting Value FailsGoogle Firestore 设置值失败
【发布时间】:2020-02-14 13:15:39
【问题描述】:

我是从 Unity 应用程序编写的 - 这不是身份验证问题,因为我尝试使用开放数据库和受限数据库。用户已通过身份验证,我在每一步都检查了代码,我看到了所有变量中的预期值,除了设置为“{}”的原始变量,而作为 JSON 的 initialUser 是 {“inventory”:[] , "保存": []}

        private void InitialiseUser(FirebaseUser user, Action<bool> callback) {
        LoadJsonAsset<UserData>("initialise/user", initialUser => {
            if (initialUser != null) {
                userDataRef.SetRawJsonValueAsync(JsonUtility.ToJson(initialUser)).ContinueWithOnMainThread(task => {
                    if (task.IsCompleted) {
                        userDataRef.GetValueAsync().ContinueWithOnMainThread(getTask => {
                            var raw = getTask.Result.GetRawJsonValue();
                            userSnapshot = getTask.Result;
                            callback(getTask.IsCompleted);
                        });
                    } else {
                        Debug.LogError("Unable to set initial user data");
                        callback(false);
                    }
                });
            } else {
                Debug.LogError("Unable to initialise user");
                callback(false);
            }
        });
    }

减少问题,使其更清晰:

        const string setRaw = "{\"inventory\":[],\"saves\":[]}";

        userDataRef.SetRawJsonValueAsync(setRaw).ContinueWithOnMainThread(setTask => {
            if (setTask.IsCompleted) {
                userDataRef.GetValueAsync().ContinueWithOnMainThread(getTask => {
                    var getRaw = getTask.Result.GetRawJsonValue();

                    Debug.Assert(getRaw == setRaw);
                });
            }
        });

我可以看到 getRaw 设置为“{}”。似乎如果您的成员是空数组,它们会被默默地从存储的数据中剥离出来,因此整个写入都会默默地失败。

如果例如 setRaw 设置为 "{\"inventory\":[\"test\"],\"saves\":[]}" 那么我们得到一个保存的记录。

【问题讨论】:

  • 我很难解析你的问题。您共享的哪一行代码没有按照您的预期执行?它有什么作用?你期待什么?还请查看how to create a minimal, complete, verifiable example,因为您共享的代码中有很多方法和变量我们不知道它们的作用/是什么。

标签: c# firebase unity3d google-cloud-firestore


【解决方案1】:

所以在回答了我自己的问题后,我将在这里记录下来以供后代使用。

Firebase.Database.DatabaseReference.SetRawJsonValueAsync 方法不会写入任何指向空数组的成员,它们只是被跳过并且默默地无法添加到数据库对象中。这在documentation 中没有提到:

将此位置的数据设置为给定的字符串 json 表示形式。

【讨论】:

    【解决方案2】:

    使用 Database 是不可能的(因为这是您删除数据的方式),但使用 Firestore 您可以IgnoreUndefinedProperties

    但是,我认为这必须从 javascript/node 完成,因为在 Unity 中没有设置此项的条目。

    const db = firestore.firestore();
    db.settings({ ignoreUndefinedProperties: true })
    

    【讨论】:

      猜你喜欢
      • 2022-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多