[英]How can I store documents in firestore database without overwritting the data?
I want to store "val credentials" inside "Credenciales" and "val generales" within "Generales" for each user in the firestore.我想为firestore中的每个用户将“val credentials”存储在“Credenciales”中,并将“val generales”存储在“Generales”中。 However, if I sign up as a new user, it overwrites the stored data of the old user.
但是,如果我注册为新用户,它会覆盖旧用户的存储数据。 Please, let me know how I can store both variables without replacing them.
请让我知道如何在不替换它们的情况下存储这两个变量。
Here's my snipped code:这是我截取的代码:
val generales = hashMapOf(
"Nombre" to nameEditText.text.toString(),
"Celular" to celularEditText.text.toString(),
"Cedula" to cedulaEditText.text.toString(),
"Oficio" to oficioEditText.text.toString(),
"Estado civil" to auto_complete_txt.text.toString(),
"Calle" to calleEditText.text.toString(),
"Sector" to sectorEditText.text.toString(),
"Edificio" to edificioEditText.text.toString(),
"Apto" to numeroEditText.text.toString(),
"Ciudad" to ciudadEditText.text.toString(),
"Residencial" to resEditText.text.toString(),
"Telefono" to telEditText.text.toString(),
)
db.collection("Usuarios").document("Generales")
.set(datos)
val credentials = hashMapOf(
"email" to emailEditText.text.toString(),
"password" to passwordEditText.text.toString(),
)
db.collection("Usuarios").document("Credenciales")
.set(credentials)
Update:更新:
val generales = hashMapOf(
"Nombre" to nameEditText.text.toString(),
"Celular" to celularEditText.text.toString(),
"Cedula" to cedulaEditText.text.toString(),
"Oficio" to oficioEditText.text.toString(),
"Estado civil" to auto_complete_txt.text.toString(),
"Calle" to calleEditText.text.toString(),
"Sector" to sectorEditText.text.toString(),
"Edificio" to edificioEditText.text.toString(),
"Apto" to numeroEditText.text.toString(),
"Ciudad" to ciudadEditText.text.toString(),
"Residencial" to resEditText.text.toString(),
"Telefono" to telEditText.text.toString(),
)
// Add a new document with a generated ID
db.collection("Usuarios").document("Credenciales")
**.add(generales)** // I get an unresolved reference error here.
.addOnSuccessListener { documentReference ->
Log.d(TAG, "DocumentSnapshot written with ID: Generales")
}
.addOnFailureListener { e ->
Log.w(TAG, "Error adding document", e)
}
You need to remove the .document("Credenciales")
when adding and just have it as:您需要在添加时删除
.document("Credenciales")
并将其设置为:
db.collection("Usuarios").add(generales) //etc.
of if you want to add to a sub collection it will be something like:如果您想添加到子集合中,它将类似于:
db.collection("Usuarios").doc(docref).collection(subcollectionRef).add(generales)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.