C# Can';t使用Firebase插入数据
在我听说Firebase之后,我决定在我的Unity小游戏中尝试一下 我阅读了文档,遵循“入门”配置教程,并更改了身份验证规则,以便每个人都可以插入数据C# Can';t使用Firebase插入数据,c#,firebase,unity3d,firebase-realtime-database,C#,Firebase,Unity3d,Firebase Realtime Database,在我听说Firebase之后,我决定在我的Unity小游戏中尝试一下 我阅读了文档,遵循“入门”配置教程,并更改了身份验证规则,以便每个人都可以插入数据 void Start() { // Set up the Editor before calling into the realtime database. FirebaseApp.DefaultInstance.SetEditorDatabaseUrl("https://magicast-bd2fc.firebaseio.co
void Start()
{
// Set up the Editor before calling into the realtime database.
FirebaseApp.DefaultInstance.SetEditorDatabaseUrl("https://magicast-bd2fc.firebaseio.com/");
Firebase.Auth.FirebaseAuth.DefaultInstance.StateChanged += DefaultInstance_StateChanged;
// Get the root reference location of the database.
DatabaseReference reference = FirebaseDatabase.DefaultInstance.RootReference;
var u = new User();
u.email = "sdjkdsdksadjsad";
u.fb_id = "sakdsajdhasd";
u.username = "jadhsjahjsda";
reference.SetValueAsync(u);
}
这段代码确实执行,但当我转到Firebase控制台时,在“数据库”选项卡上看不到任何数据。我不知道我做错了什么,代码没有给出任何错误!我做错了什么
这是我的用户类。它基本上是一个测试类,没有什么大问题:
public class User
{
public string username;
public string email;
public string fb_id;
public User()
{
}
public User(string username, string email, string fb_id)
{
this.username = username;
this.email = email;
this.fb_id = fb_id;
}
}
您未通过身份验证,默认Firebase安全规则要求
auth!=空
有关如何使用Unity进行身份验证,请参阅。您不能对Firebase数据库的任意对象调用.set()
,它们必须属于以下类别之一:
string
long
double
bool
字典
列表
reference.SetValueAsync(u);
你需要打电话
string json = JsonUtility.ToJson(u);
reference.SetRawJsonValueAsync(json);
您也没有实际登录任何用户。要登录匿名用户,请首先转到Firebase控制台->身份验证并打开“匿名”开关。然后使用以下代码进行身份验证:
Firebase.Auth.FirebaseAuth auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
auth.SignInAnonymouslyAsync().ContinueWith(task => {
if (task.IsCanceled) {
Debug.LogError("SignInAnonymouslyAsync was canceled.");
return;
}
if (task.IsFaulted) {
Debug.LogError("SignInAnonymouslyAsync encountered an error: " + task.Exception);
return;
}
Firebase.Auth.FirebaseUser newUser = task.Result;
Debug.LogFormat("User signed in successfully: {0} ({1})",
newUser.DisplayName, newUser.UserId);
});
正如我在问题上所说,我更改了身份验证规则,以便每个人都可以插入数据。我目前的规则是:“读”:“真”,“写”:“真”@ArturoTemplário在这种情况下,很可能是
用户
对象。您可以对简单值或json字典类型结构调用.set()
,而不是对任意对象。你需要调用stringjson=JsonUtility.ToJson(u)然后是reference.SetRawJsonValueAsync(json)代码>-来自文档。我确实进行了json转换,但仍然不起作用。好的,我等着,先谢谢你