繁体   English   中英

如何仅将文档从一个集合复制到另一个集合一次 Flutter Firebase

[英]How can I copy documents from one collection to the other only once Flutter Firebase

我想从 Firebase 的子集合中获取特定数据,并将其仅复制一次(因此没有流)到另一个集合。 如何修复代码以使其正常工作? 可能我需要用 getDocuments() 改变一些东西,但我不知道如何使它工作。 谢谢你的帮助。

  void copy() async {
 await  _firestore
    .collection('users')
    .document(loggedinUser.uid)
    .collection('Dates')
    .document(yesterdayDate)
    .collection(widget.reference)
    .getDocuments().then((QuerySnapshot snapshot) {snapshot.documents.forEach((message) {

      final getitem = message.data['item'];
    final getarchieved = message.data['archived'];
    final getchecked = message.data['checked'];}) {




    if (getchecked == false && widget.reference == 'Todo') {
      _firestore
          .collection('users')
          .document(loggedinUser.uid)
          .collection('Dates')
          .document(selectedDate)
          .collection(widget.reference)
          .add({
        'item': getitem,
        'archived': getarchieved,
        'checked': getchecked
      });
    } else if (widget.reference == 'Goals') {
      _firestore
          .collection('users')
          .document(loggedinUser.uid)
          .collection('Dates')
          .document(selectedDate)
          .collection(widget.reference)
          .add({
        'item': getitem,
        'archived': getarchieved,
        'checked': false
      });
    }



  }
 }

好的,经过大量的研究和反复试验,我终于有了一个可行的解决方案。 我希望我可以通过这个解决方案帮助面临同样问题的人:

void copy() async {
QuerySnapshot snaphsot = await _firestore
    .collection('users')
    .document(loggedinUser.uid)
    .collection('Dates')
    .document(yesterdayDate)
    .collection(widget.reference)
    .getDocuments();

for (var message in snaphsot.documents) {
  final getitem = message.data['item'];
  final getarchieved = message.data['archived'];
  final getchecked = message.data['checked'];

  if (getchecked == false && widget.reference == 'Todo') {
    _firestore
        .collection('users')
        .document(loggedinUser.uid)
        .collection('Dates')
        .document(selectedDate)
        .collection(widget.reference)
        .add({
      'item': getitem,
      'archived': getarchieved,
      'checked': getchecked
    });
  } else if (widget.reference == 'Goals') {
    _firestore
        .collection('users')
        .document(loggedinUser.uid)
        .collection('Dates')
        .document(selectedDate)
        .collection(widget.reference)
        .add({'item': getitem, 'archived': getarchieved, 'checked': false});
  }
 }
}

解决方案很棒,它帮助我解决了我的问题。 在我的情况下,我想要一个 function,它允许我将文档从一个集合移动到另一个集合。

但是我也想在将旧文档复制到新集合后将其删除,并且不想迭代抛出的文档。 我后来遇到了一种情况,没有提供的 id 文档被复制,所以我不得不为此抛出一个错误。

这就是为什么我决定也在这里发布我的 function,以便人们可以从 Felix 和我的 function 中选择相关部分,其中包含针对没有可用文档的复制、删除、检测和错误处理。

moveDocumentToOtherCollection(String docId) async {

    var oldColl = db.collection('oldcollection').doc(docId);
    var newColl = db.collection('newcollection').doc(oldColl.id);


    DocumentSnapshot snapshot = await oldColl.get()
    // ignore: missing_return
    .then((docSnapshot) {
    if (docSnapshot.exists) {

    // document id does exist
    print('Successfully found document');

    newColl
      .set({
        'name': docSnapshot['name'],
        'message': docSnapshot['message'],
        'id': docSnapshot['id'],
        'isnew': docSnapshot['isnew'],
        'dateWhenProofed': DateTime.now(),
        'approved': true
        
      })
      .then((value) => print("document moved to different collection"))
      .catchError((error) => print("Failed to move document: $error")).then((value) => ({

    oldColl
      .delete()
      .then((value) => print("document removed from old collection"))
      .catchError((error) {

        newColl.delete();
        print("Failed to delete document: $error");

      })
      }));

    } else {

      //document id doesnt exist
      print('Failed to find document id');
    }
    });
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM