我实际上可以使用Maps插入数据,但我不能使用我自己的类中的对象。在火库文件中,他们说我可以。
错误是:
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")
}
}
}
}
}
答案 0 :(得分:0)
正如here
所解释的那样每个自定义类必须具有不带参数的公共构造函数。此外,该类必须包含每个属性的公共getter。
答案 1 :(得分:0)
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