【问题标题】:Firebase Document Fields are getting DeletedFirebase 文档字段被删除
【发布时间】:2023-04-09 04:23:01
【问题描述】:

一段时间以来,我注意到我的一些 firebase 文档的字段被删除,即使我没有删除它们或在我的应用程序上编写任何逻辑来删除文档或文档本身的字段。 This is a picture that shows a document on my firebase project, which has its fields deleted on its own.

firebase 项目已连接到我的颤振应用程序,但我没有编写任何删除功能。 我只是偶尔用最近的数据更新这些字段。 请问,谁知道发生了什么?

编辑

这就是我更新用户集合文档的方式

Future<String?> submitUpdate(User user) async{
CollectionReference userCollection = firestore.collection('users');
String? uid = await secureStorage.read(key: 'uid');
try{
  
  //updates user doc in users collection
  await userCollection.doc(uid).update(user.toMap());
  return "Success";
}catch(e){
  return null;
}

这就是我在用户集合中创建用户文档的方式。

Future<String?> saveUserCredentials(User user) async{
CollectionReference users = firestore.collection('users');
String? uid = await FlutterSecureStorage().read(key: "uid");

try{
  //creates a user doc in the users collection
  await users.doc(uid).set(
    user.toMap()
  );
  return "Success";
}catch (e){
  print(e);
  return null;
}

我有一个定义用户对象的用户模型。

【问题讨论】:

  • 您能否发布您的代码以创建和更新。可能是更新代码有问题。
  • 我已经做到了,谢谢。

标签: firebase flutter firebase-realtime-database google-cloud-firestore flutter-getx


【解决方案1】:

如果您想设置文档的某些字段,但保留现有字段,请使用 setSetOptions(merge: true)

FirebaseFirestore.instance
  .collection(...)
  .doc(...)
  .set({'field1': value1, 'field2': value2}, SetOptions(merge: true));

没有这个,set 中没有列出的现有字段将不会被保留,可能你遇到过这种情况。

【讨论】:

    猜你喜欢
    • 2022-01-01
    • 1970-01-01
    • 2021-01-01
    • 2018-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多