如何保存到Firebase数据库?

时间:2018-10-08 14:28:25

标签: c# firebase firebase-realtime-database

最近几天,我一直在尝试写入Firebase数据库。我是C#的新手,但是我已经阅读了Firebase有关如何保存数据的所有文档。我几天前问了一个问题,并使用收到的建议创建了此代码,但是尽管它写了每个目录,但保存的实际数据显示为{}。

任何建议我可以做些什么才能使其正确编写?

谢谢,但是我使用了这段代码,结果仍然是{}。知道有什么问题吗?我正在使用Unity,并且当有人单击“提交”按钮时正在调用AddNewUserData。我最终将用存储用户电子邮件地址的emailText.text替换“ email”,但是现在我只是试图创建一个工作脚本以将数据写入数据库,因此除了关注之外,我什么都不关心能够以任何方式将有效数据写入数据库。请帮忙。

public class User
{               
    public int round { get; set; }
    public int lives { get; set; }
    public int level { get; set; }
    public int currentScore { get; set; }
    public int totalScore { get; set; }
    public User(int round, int lives, int level, int currentScore, int totalScore)
    {

        this.round = round;
        this.lives = lives;
        this.level = level;
        this.currentScore = currentScore;
        this.totalScore = totalScore;
    }
}
  public void AddNewUserData()
  {

    DatabaseReference root = FirebaseDatabase.DefaultInstance.RootReference;
    var json = JsonUtility.ToJson(new User(1, 3, 1, 0, 0));
    root.Child("users").Child("email").SetValueAsync(json);

}

1 个答案:

答案 0 :(得分:1)

根据文档https://firebase.google.com/docs/database/unity/save-data,请尝试SetRawJsonValueAsync

<div class="container">
   <div class="row">
      <div class="card-columns">
         <div class="card card-pin" th:each="product : ${Products}">
            <img class="card-img" id="productDetail" th:src="${product.href}" alt="Card image" th:onclick="'getProductDetail(\'' + ${product.itemNumber} + '\')'" />
            <span class="text-justify" th:text="${product.name}">productName</span>
         </div>
      </div>
   </div>
</div>