简体   繁体   English

Firebase 实时数据库不更新数据

[英]Firebase Realtime database not updating data

I am new to Firebase and I am trying to learn its database.我是 Firebase 的新手,我正在尝试学习它的数据库。 This is what my code looks like.这就是我的代码的样子。

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.example.roomdemo.databinding.ActivityMainBinding
import com.google.firebase.database.FirebaseDatabase

class MainActivity : AppCompatActivity() {
    private  var binding: ActivityMainBinding? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding?.root)

        binding?.button?.setOnClickListener {
            val rootNode = FirebaseDatabase.getInstance()
            val reference = rootNode.getReference("Users")
            reference.setValue("hello")
        }

    }
}

I am simply trying to set the value 'hello' to my node 'Users'.我只是想将值“hello”设置为我的节点“Users”。 But Somehow it's not working.但不知何故它不起作用。 Are there any configuration settings that I missed?我是否遗漏了任何配置设置? Please help me.请帮我。 Also pasting a screenshot of how the database looks like Database image还粘贴了数据库看起来像数据库图像的屏幕截图

Please help to connect my code with the database请帮助将我的代码与数据库连接

This is how my rules looks like这就是我的规则的样子

{
  "rules": {
    ".read": "true",
    ".write": "true"
  }
}

To see if something goes wrong, you have to attach a listener and handle exceptions:要查看是否出现问题,您必须附加一个侦听器并处理异常:

reference.setValue("hello").addOnCompleteListener {
    if (it.isSuccessful) {
        Log.d"(TAG", "Data added successfully.")
    } else {
        //Never ignore potential errors!
        Log.d("TAG", "Failed with: ${it.exception?.message}")
    }
}

If the above operation doesn't add anything to the database, it's most likely because the Firebase servers reject the operation due to improper security rules.如果上述操作没有向数据库添加任何内容,则很可能是因为 Firebase 服务器由于不正确的安全规则而拒绝了该操作。 So you have set the proper rules in your Firebase console.因此,您已在 Firebase 控制台中设置了正确的规则

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM