【发布时间】:2021-01-21 02:56:16
【问题描述】:
我有用于创建或更新客户的功能。
我能够成功写入数据库。我创建了一个名为 user_id 的字段,我将 currentUser uid 保存到其中,因此我只能读取已登录的用户文档。
但是,我无法更新文档,因为我知道我可能没有以正确的方式引用文档。
我收到以下错误:
颤振:错误:PlatformException(错误5,FIRFirestoreErrorDomain,否 要更新的文档:
我做错了什么?
下面是我的函数:
Future createOrUpdateCustomer(Customer customer, bool isUpdating) async {
FirebaseUser user = await FirebaseAuth.instance.currentUser();
String userId = user.uid;
print('Current logged in user uid is: $userId');
CollectionReference customerRef =
await Firestore.instance.collection('customers');
if (isUpdating) {
customer.updatedAt = Timestamp.now();
customer.userId = userId;
await customerRef.document().updateData(customer.toMap());
print('updated customer with id: ${customer.id}');
print('updated customer with logged in uid: ${customer.userId}');
} else {
customer.createdAt = Timestamp.now();
DocumentReference documentReference = customerRef.document();
customer.id = documentReference.documentID;
customer.userId = userId;
print('created customer successfully with id: ${customer.id}');
await documentReference.setData(customer.toMap(), merge: true);
addCustomer(customer);
}
notifyListeners();
}
【问题讨论】:
标签: firebase flutter google-cloud-firestore firebase-authentication flutter-dependencies