[英]Why can't I insert an object into set firebase.firestore Android Studio?
实际上,我可以使用Maps插入数据,但是不能使用自己类中的对象。 在Firestore文档中时,他们说我可以。
错误是:
java.lang.RuntimeException: No properties to serialize found on class com.example.alumne.rateit.core.model.Restaurant
任何帮助,将不胜感激,谢谢。
class Restaurant(name: String, fiscName: String, cif: String, addr: String, city: String, phone: String, zipCode: String, country: String, email: String): Serializable{ }
override fun onClick(view: View?) {
when(view?.id){
btn_submit.id ->{
if (checkForEmptyField()){
val restName = et_rest_name.text.toString()
val restFiscName = et_fiscal_name.text.toString()
val restAddr = et_address.text.toString()
val restPhone = et_phone.text.toString()
val restZipCode = et_zip_code.text.toString()
val restCif = et_cif.text.toString()
val restCity = et_city.text.toString()
val restCountry = et_country.text.toString()
val restEmail = et_email.text.toString()
/*val restaurant: HashMap<String, String> = HashMap()
restaurant.put("name",restName)
restaurant.put("fiscName",restFiscName)
restaurant.put("address",restAddr)
restaurant.put("phone",restPhone)
restaurant.put("zipCode",restZipCode)
restaurant.put("cif",restCif)
restaurant.put("city",restCity)
restaurant.put("country",restCountry)
restaurant.put("email",restEmail)*/
val rest = Restaurant(restName,restFiscName,restCif,restAddr,restCity,restPhone,restZipCode,restCountry,restEmail)
if (bd.collection("restaurants").document().set(rest).isSuccessful){
Utilities.showMessage(view,"Restaurant added correctly")
}else{
Utilities.showMessage(view,"Error adding restaurant")
}
}else{
if (view != null) {
Utilities.showMessage(view,"Fill all fields")
}
}
}
}
}
正如解释这里
每个自定义类都必须具有不带参数的公共构造函数。 另外,该类必须为每个属性包含一个公共获取程序。
Firebase不接受set方法的对象。 您可以简单地使用接口,或将类对象转换为接口,如下所示。 创建一个这样的接口和类。
export interface IUser {
name: string;
email: string;
id: string;
}
export class User implements IUser {
name: string;
email: string;
id: string;
constructor(args) {
this.name = args.name;
this.email = args.email;
this.id = args.id;
}
}
然后,使用以下命令将对象转换为接口实例
var userData = new User({
name: "user",
email: "user@gmail.com",
id: ":LKJDFasdfFSDsadfhAdfsf76dfasdf",
});
var user: IUser = <IUser>JSON.parse(JSON.stringify(userData));
您可以在set方法中使用此User模型。
this.firestore.collection('users').doc(this.userData.id).set(user);
有关更多信息,请访问Firestore官方文档: https : //firebase.google.com/docs/firestore/manage-data/add-data
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.