【问题标题】:Why can't I insert an object into set firebase.firestore Android Studio?为什么我不能在 set firebase.firestore Android Studio 中插入对象?
【发布时间】:2018-01-20 03:22:14
【问题描述】:

我实际上可以使用地图插入数据,但我不能使用我自己的类中的对象。在 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")
                }
            }
        }
    }
}

【问题讨论】:

  • 你能发布你收到的错误信息吗?
  • java.lang.RuntimeException:在类 com.example.alumne.rateit.core.model.Restaurant 上找不到要序列化的属性
  • 用 Serializable 扩展 Restaurant 类。
  • 告诉我们Restaurant:错误说它不包含序列化它的属性...
  • 请编辑您的问题以显示您的餐厅课程的内容。另外,请记住 set() 方法是异步的并返回一个未解析的任务,您需要将侦听器附加到该任务以知道它何时完成。

标签: java android firebase kotlin google-cloud-firestore


【解决方案1】:

正如here解释的那样

每个自定义类都必须有一个不带参数的公共构造函数。此外,该类必须为每个属性包含一个公共 getter。

【讨论】:

    【解决方案2】:

    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

    【讨论】:

      猜你喜欢
      • 2017-02-17
      • 1970-01-01
      • 1970-01-01
      • 2022-12-03
      • 1970-01-01
      • 1970-01-01
      • 2016-10-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多