【问题标题】:Unable to update complete child object in Firebase Java无法在 Firebase Java 中更新完整的子对象
【发布时间】:2020-02-05 15:35:49
【问题描述】:

我有两个课程

@Data
public static class Person {
    private String id;
    private String value;
    private Address address;

}

@Data
public static class Address {
    private String add1;
    private String add2;
}

@Test
void name2() throws ExecutionException, InterruptedException {

    final Person p = new Person();
    p.setId("2FEpIFGqH7izWJ1ydfaKtg");
    p.setValue("some-value");

    final Address address = new Address();
    address.setAdd1("Add 1");
    address.setAdd2("Add 2");
    p.setAddress(address);

    final Map<String, Object> params = new HashMap<>();
    params.put("address", address);

    firestore.collection("test").document("2FEpIFGqH7izWJ1ydfaKtg").update(params).get();

    // firestore.collection("test").document("2FEpIFGqH7izWJ1ydfaKtg").set(params, SetOptions.merge()).get(); This also doesn't works, I get same error.
}

我在运行此代码时收到以下错误


com.google.cloud.firestore.FirestoreException: Cannot convert CustomerRepositoryTest.Address(add1=Add 1, add2=Add 2) to Firestore Value
at com.google.cloud.firestore.FirestoreException.invalidState(FirestoreException.java:51)
    at com.google.cloud.firestore.UserDataConverter.encodeValue(UserDataConverter.java:178)
    at com.google.cloud.firestore.DocumentSnapshot.fromObject(DocumentSnapshot.java:89)
    at com.google.cloud.firestore.UpdateBuilder.performUpdate(UpdateBuilder.java:513)
    at com.google.cloud.firestore.UpdateBuilder.update(UpdateBuilder.java:347)
    at com.google.cloud.firestore.DocumentReference.update(DocumentReference.java:231)

但是,我可以通过以下路径访问文档来更新 add1 的部分数据设置值:/test/&lt;ID&gt;/address.add1

我想要一种机制来更新完整的对象,而不仅仅是特定的值。 有人可以帮忙吗?

【问题讨论】:

  • 请将您的数据库结构添加为屏幕截图,并指出发生该错误的确切代码行。

标签: java firebase google-cloud-platform google-cloud-firestore


【解决方案1】:

要写入整个对象,请使用ref.set(p)。这将用p 中的数据覆盖ref 中的所有现有数据。

无法将对象的数据子集写入某个位置。如果您需要该功能,则需要创建一个 Map,其中包含您要更新的特定字段。

【讨论】:

  • 感谢您的建议,但它没有满足我的要求,它不仅更新了 Address,还将其他值设置为 null。但我在您的建议后找到了解决方案,为了更新子对象,我们需要使用 SetOptions.merge(fieldNames)。 code firestore.collection("test").document("EpIFGqH7izWJ1ydfaKtg").set(test, SetOptions.mergeFields("address")).get();
  • 有趣。如果您在使用该 API 时发布带有更新代码的自我回答,这可能是值得的。
【解决方案2】:

终于找到解决办法了:

@Test
void name2() throws ExecutionException, InterruptedException {

    final Person p = new Person();

    final Address address = new Address();
    address.setAdd1("Add 1");
    address.setAdd2("Add 2");
    p.setAddress(address);

    final Map<String, Object> params = new HashMap<>();
    params.put("address", address);

    firestore.collection("test").document("2FEpIFGqH7izWJ1ydfaKtg").set(test, SetOptions.mergeFields("address")).get();

}

通过使用 SetOptions.mergeFields() 并提供要更新的字段,我能够更新指定的字段。

【讨论】:

  • 很高兴看到它奏效了。另外,我将分享这个documentation 关于这个'firebase.firestore.SetOptions'。另外,我建议您接受自己的答案。
猜你喜欢
  • 1970-01-01
  • 2020-01-12
  • 1970-01-01
  • 2014-05-17
  • 2021-05-25
  • 2023-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多