C# Firebase检索空数据

C# Firebase检索空数据,c#,unity3d,firebase-realtime-database,C#,Unity3d,Firebase Realtime Database,我正在尝试从Firebase实时数据库检索数据。但结果总是空的。我不知道结果为空的原因。我有一个firebase manager脚本,我只是从该脚本调用变量。这是我第一次做firebase,也是我在unity的第一个游戏项目。希望有人能启发我 public class Player : Monobehaviour { public FirebaseUser user; public DatabaseReference reference; public int level; public in

我正在尝试从Firebase实时数据库检索数据。但结果总是空的。我不知道结果为空的原因。我有一个firebase manager脚本,我只是从该脚本调用变量。这是我第一次做firebase,也是我在unity的第一个游戏项目。希望有人能启发我

public class Player : Monobehaviour 
{
public FirebaseUser user;
public DatabaseReference reference;
public int level;
public int exp;
public Text levelText;
public Text expText;

void Start()
{
    user = GameObject.Find("Firebase Manager").GetComponent<FirebaseManager>().User;
    reference = GameObject.Find("Firebase Manager").GetComponent<FirebaseManager>().DBreference;
    StartCoroutine(LoadUserData());
}

void Update()
{
   levelText.text = level.ToString();
   expText.text = exp.ToString();
}

private IEnumerator LoadUserData()
{
    var DBTask = reference.Child("users").Child(user.UserId).GetValueAsync();

    yield return new WaitUntil(predicate: () => DBTask.IsCompleted);

    if(DBTask.Exception != null)
    {
        Debug.LogWarning(message: $"Failed to register task with {DBTask.Exception}");
    }
    else if(DBTask.Exception == null)
    {
       level = 1;
       exp = 0;
       Debug.Log("NullResult");
    }
    else
    {
        DataSnapshot snapshot = DBTask.Result;

        level = int.Parse(snapshot.Child("Level").ToString());
        exp = int.Parse(snapshot.Child("Exp").ToString());
    }

   
}
}
公共类玩家:单行为
{
公共FirebaseUser用户;
公共数据库参考;
公共智力水平;
公共进出口;
公共文本层面的文本;
公共文本;
void Start()
{
user=GameObject.Find(“Firebase管理器”).GetComponent().user;
reference=GameObject.Find(“Firebase管理器”).GetComponent().DBreference;
start例程(LoadUserData());
}
无效更新()
{
levelText.text=level.ToString();
expText.text=exp.ToString();
}
私有IEnumerator LoadUserData()
{
var DBTask=reference.Child(“用户”).Child(user.UserId).GetValueAsync();
yield返回新的WaitUntil(谓词:()=>DBTask.IsCompleted);
if(DBTask.Exception!=null)
{
Debug.LogWarning(消息:$“未能向{DBTask.Exception}注册任务”);
}
else if(DBTask.Exception==null)
{
级别=1;
exp=0;
Log(“NullResult”);
}
其他的
{
DataSnapshot snapshot=DBTask.Result;
level=int.Parse(snapshot.Child(“level”).ToString();
exp=int.Parse(snapshot.Child(“exp”).ToString());
}
}
}

您的代码的编写方式永远不会执行第三个块:

if(DBTask.Exception != null)
{
    Debug.LogWarning(message: $"Failed to register task with {DBTask.Exception}");
}
else if(DBTask.Exception == null)
{
   level = 1;
   exp = 0;
   Debug.Log("NullResult");
}
else
{
    DataSnapshot snapshot = DBTask.Result;

    level = int.Parse(snapshot.Child("Level").ToString());
    exp = int.Parse(snapshot.Child("Exp").ToString());
}
查看第二个
if
如何与第一个相反?这可能不是您想要的,但它确保无条件的
else
块永远不会执行

我猜你想要第二个条件如下:

else if(DBTask.Result.Exists)
因此,如果任务已完成,但数据库中的路径上没有数据,则第二个块将运行


注意:可能会出现打字错误和小错误,因此在测试时请准备好参考文档:。

这样就可以了。我注意到您忘记了.Value.ToString();如果你不把这个值放在ToString之前,它肯定不会工作

else if(DBTask.Result.Exist)
{
   DataSnapshot snapshot = DBTask.Result;
   level = int.Parse(snapshot.Child("Level").Value.ToString());
   exp = int.Parse(snapshot.Child("Exp").Value.ToString());
 }

谢谢@Frank van Puffelen先生,结果不再为空。但另一个问题出现了。exp和level的检索数据始终为0。即使在数据库中,exp和level也不是0。为什么if
if(DBTask.Exception==null)
不使用结果?这不是预期的结果吗?