实时数据库未将数据推送到 Firebase
Posted
技术标签:
【中文标题】实时数据库未将数据推送到 Firebase【英文标题】:Realtime database not getting data pushed into firebase 【发布时间】:2021-08-28 20:49:25 【问题描述】:我正在使用实时数据库来保存用户信息,但问题是它根本不工作,同时存储和云火库正常工作,尝试更改规则,尝试不同的方法但没有一个真正有效,如果有人可以帮助谢谢你提前。
这是我的代码
class CustomerRegistrationActivity : AppCompatActivity()
private lateinit var imageDownloadUrl : String
private var imageUrl : Uri? = null
private lateinit var firebaseAuth: FirebaseAuth
private lateinit var databaseReference: DatabaseReference
private lateinit var storageReference: StorageReference
private var _binding : ActivityCustomerRegistrationBinding? = null
private val binding get() = _binding!!
override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)
_binding = ActivityCustomerRegistrationBinding.inflate(layoutInflater)
setContentView(binding.root)
init()
binding.register.setOnClickListener
binding.register.startAnimation()
registerNewCustomer()
binding.customerImg.setOnClickListener
selectCustomerImage()
binding.haveAccount.setOnClickListener
Intent(this, CustomerLoginAcitivity::class.java).apply
startActivity(this)
finish()
private fun init()
firebaseAuth = FirebaseAuth.getInstance()
databaseReference = FirebaseDatabase.getInstance().reference
storageReference = FirebaseStorage.getInstance().reference
private fun registerNewCustomer()
val customerUserName = binding.userName.text.toString()
val customerPhone = binding.phone.text.toString()
val customerEmail = binding.email.text.toString()
val customerPassword = binding.password.text.toString()
if(TextUtils.isEmpty(customerUserName))
Toast.makeText(this,"Missing Username..",Toast.LENGTH_SHORT).show()
return
if(TextUtils.isEmpty(customerPhone))
Toast.makeText(this,"Missing phone..",Toast.LENGTH_SHORT).show()
return
if(TextUtils.isEmpty(customerEmail))
Toast.makeText(this,"Missing Email..",Toast.LENGTH_SHORT).show()
return
if(TextUtils.isEmpty(customerPassword))
Toast.makeText(this,"Missing password..",Toast.LENGTH_SHORT).show()
return
lifecycleScope.launch(Dispatchers.Main)
try
// creating user account
firebaseAuth.createUserWithEmailAndPassword(customerEmail,customerPassword).await()
// check if image url is not null
imageDownloadUrl = if(imageUrl == null)
""
else
// if url not null , push into firestorage
val task = storageReference.child("Customers")
.child(firebaseAuth.currentUser?.uid!!).putFile(imageUrl!!)
task.await().storage.downloadUrl.toString()
// creating a map for user info
val credentialsMap = hashMapOf<String,Any>()
credentialsMap["customerName"] = customerUserName
credentialsMap["customerEmail"] = customerEmail
credentialsMap["CustomerPhone"] = customerPhone
credentialsMap["accountType"] = "Customer"
credentialsMap["profileImage"] = imageDownloadUrl
// pushing data into realtime database
// the issue is here , i even debugged the code , it executes but don't pass to
// to the next code and it is not saving data
databaseReference
.child("customers")
.child(firebaseAuth.currentUser?.uid!!)
.setValue(credentialsMap).await()
// the code here gets never exeucte but there is issue with code above of real db
binding.register.stopAnimation()
CoroutineScope(Dispatchers.Main).launch
delay(1000)
Intent(this@CustomerRegistrationActivity,CustomerMainActivity::class.java).apply
startActivity(this)
finish()
catch (ex : Exception)
binding.register.stopAnimation()
Log.d(Utils.ACTIVITY_TAG,"Exception Registration $ex.message")
private fun selectCustomerImage()
Intent().apply
type = "image/*"
action = Intent.ACTION_GET_CONTENT
startActivityForResult(this, IMAGE_REQUEST_CODE)
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?)
super.onActivityResult(requestCode, resultCode, data)
if(requestCode == IMAGE_REQUEST_CODE)
data?.data.let
imageUrl = it
binding.customerImg.setImageURI(imageUrl)
override fun onDestroy()
super.onDestroy()
_binding = null
companion object
const val IMAGE_REQUEST_CODE = 1001
实时数据库规则
"rules":
".read": "auth !== null",
".write": "auth !== null"
【问题讨论】:
您可能需要addOnFailureListener
将值设置为firebase 并记录回调消息以查看错误
【参考方案1】:
尝试在 push 时删除 .await() 并将 db 规则设置为 true
".read": true,
".write": true
【讨论】:
我确实将它设置为 true 并且它不起作用,它现在起作用但真的很奇怪,我已经重建并清理了项目,它起作用了。不知道为什么,谢谢你的回答和你的时间伙伴。".read": true
和 ".write": true
有各自的后果。设置读写权限 true 意味着简单地授予公共访问权限以读取和修改(甚至删除)数据库。请使用适当的安全规则来保护您的数据库免受恶意活动的侵害。如需详细了解Firebase 安全规则的最佳做法,请访问firebase.google.com/docs/rules/insecure-rules以上是关于实时数据库未将数据推送到 Firebase的主要内容,如果未能解决你的问题,请参考以下文章
如何在不在 firebase 实时数据库上生成 firebase id 的情况下发布我的数据?
如何将数据从 Informix 实时推送到 .NET 服务器?