【问题标题】:I am trying to access the values of a particular key from the Firebase database.I want to update those values and update it我正在尝试从 Firebase 数据库中访问特定键的值。我想更新这些值并更新它
【发布时间】:2019-08-01 18:01:07
【问题描述】:

[这是我的json数据This is my code][1]

我正在尝试更改 Firebase 数据库中键值对的值。 假设我有一个像这样的键值:“化学”:“20”。我想提取键化学的值,并想在添加一些数字后更新它,比如我加 10,所以我的最终键值对将变成 "chemistry":"30" 。但是我无法访问密钥,有人可以帮我解决这个问题。

我的代码放在这里:

 mAuth.signInWithEmailAndPassword(email, password)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            // Sign in success, update UI with the signed-in user's information
                            Log.d("TAG", "signInWithEmail:success");
                            //extracting the current user from the databse
                            FirebaseUser user = mAuth.getCurrentUser();
                            //getting the uid of the user
                            String Uid=user.getUid();
                            //getting the databse instance for the particular user
                            FirebaseDatabase database = FirebaseDatabase.getInstance();
                            //setting the path to the Users/Uid to read the key values
                            DatabaseReference myRef = database.getReference("Users/"+Uid);


                            //textView.setText(myRef);
                            String key=myRef.child("chemistry").getKey();
                            textView.setText(key);

                           // myRef.setValue("Hello, World!");
                        } else {
                            // If sign in fails, display a message to the user.
                            Log.w("TAG", "signInWithEmail:failure", task.getException());
                            Toast.makeText(TestActivity.this, "Authentication failed.",
                                    Toast.LENGTH_SHORT).show();
                            //updateUI(null);
                        }

                        // ...
                    }
                });

我的 json 在这里:

{
  "Users" : {
    "LMn5GeZixNQNpdQhGzdKNbTgIw53" : {
      "chemistry" : "20",
      "email" : "umairnsr87@gmail.com",
      "lastScore" : "0",
      "maths" : "0",
      "name" : "umair",
      "phone" : "123456789",
      "physics" : "0"
    }
  }
}

【问题讨论】:

    标签: java android firebase firebase-realtime-database


    【解决方案1】:

    一般来说,您应该添加一些您想要达到的目标的尝试代码,以便其他人可以更好地指导您解决问题。人们不应该为你写代码。

    无论如何,如果你的收藏被称为用户。那么您将通过这种方式检索特定 ID:

    db.collection("users").doc(THEID).get()
        .then( data => {
           console.log(data)
        })
    

    使用 .add(jsondata) 和 .set(jsondata) 进行添加和更新

    这是文档: https://firebase.google.com/docs/firestore/?gclid=CjwKCAjwm4rqBRBUEiwAwaWjjK7_CnO37twhvxrw2-WUxep6ykpTEaJiGnmojoBz74TNylodfkL7DxoCE9gQAvD_BwE

    【讨论】:

    • 我使用的是 firebase 实时数据库,而不是云存储。
    【解决方案2】:

    我正在尝试更改 firebase 数据库中键值对的值。

    一般当你想更新一个属性的值时,你应该使用DatabaseReference的updateChildren(Map update)

    将特定的子键更新为指定的值。

    关于第二个问题:

    假设我有一个像这样的键值:“化学”:“20”。我想提取键化学的值,并想在添加一些数字后更新它,比如我向它添加 10,所以我的最终键值对将变为 "chemistry":"30"

    上面,我向您展示了如何简单地更新属性的值,但是,如果您想在多用户环境中增加 chemistry 字段的值以获得一致的数据,我建议您查看我的答案从这个 post,我解释了如何使用 Firebase Transactions 更新分数字段。

    【讨论】:

      猜你喜欢
      • 2018-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-07
      • 2020-10-28
      • 1970-01-01
      • 2022-01-08
      • 1970-01-01
      相关资源
      最近更新 更多