将文档引用列表添加到 flutter/firebase 文档字段中

问题描述 投票:0回答:1

我得到了这个代码:

  var testList = <String>[];

  await db
      .collection('tests')
      .where('user', arrayContainsAny: [db.doc('users/' + uid)])
      .get()
      .then((value) {
        value.docs.forEach((element) {
          testList.add(element.id);
        });
      });

  for (final test in testList) {
    print(test);
  }

运行顺利,我得到了 2 个测试 ID 的测试列表。后来我尝试这个:

        final testCollectionEntries = {
          'tests': db.doc('tests/' + testList),
          'name': testCollectionName
        };

        await db.collection("testsCollection").add(testCollectionEntries );

tests 是 firebase 中文档引用列表的字段类型。我如何制作文档参考列表以便将其用于此代码。

flutter firebase list google-cloud-firestore
1个回答
0
投票

我发现这是一个答案。您需要像这样编写代码:

final testCollectionEntries = {
    //'tests': db.doc('tests/' + testList), //REMOVE THIS IN ORDER TO USE UPDATE LATER
    'name': testCollectionName
    };
       
DocumentReference testRef = //ADD THIS VAR
   await db.collection("testsCollection").add(testCollectionEntries);

await db
    .collection('test')
    .where('user', arrayContainsAny: [db.doc('users/' + uid)])
    .get()
    .then((value) {  //USE THEN FOR ITERATION
      value.docs.forEach((element) {
        print(db.doc('tests/' + element.id));
        testRef.update({
          'examinerList':
              FieldValue.arrayUnion([db.doc('examiners/' + element.id)]) //DONT FORGET BRACKETS []
        });
      });
    });
© www.soinside.com 2019 - 2024. All rights reserved.