我得到了这个代码:
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 中文档引用列表的字段类型。我如何制作文档参考列表以便将其用于此代码。
我发现这是一个答案。您需要像这样编写代码:
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 []
});
});
});