Firebase批量写入不存在的顶级集合

huangapple go评论69阅读模式
英文:

Firebase batch write non-existent top-level collections

问题

在我的Vue3 + Firebase Web应用中,我有一个上传包含学校学生信息的.csv文件的功能。

文件被读取并保存到一个对象中。
这个.csv表的每一行都有一个名为“courseName”的列。
通过点击一个按钮,我有一个函数,它使用一个for循环和Firebase批量写入来将.csv写入到Firestore中。

“courseName”列很重要,因为我的Firestore结构如下:

schools/someID/courses/courseName/students/

该函数循环遍历.csv文件,并将行添加到批量写入中。当courseName更改时,提交批处理、清除批处理,并继续为共享相同courseName的下一个学生创建新的批处理。

批量写入本身的逻辑如下所示:

const pathRef = doc(db, "schools/" + userInfoStore.userInfo?.schoolID + '/courses/' + currentCourse + '/students/' + val[columnNames.value.UID])

const data = {
    vorname: val[columnNames.value.Vorname],
    nachname: val[columnNames.value.Nachname],
    klasse: val[columnNames.value.Klasse],
    uid: val[columnNames.value.UID]
}

batch.set(pathRef, data)

这个方法可以工作,但它会在数据库中留下名为“currentcourse”的集合,意味着它本身并不存在,并且无法在Firestore查询中找到。

我该如何防止这种情况发生?

英文:

In my Vue3 + Firebase Web-App i have an upload for a .csv that contains students of a school.

The file is read and saved to an object.
Each row of this .csv table has a column called "courseName".
With the click of a button i have a function that uses a for-loop and
firebase batch writing to write the .csv to firestore.

The "courseName" column is important as my firestore structure looks as follows:

schools/someID/courses/courseName/students/

The function loops through the .csv and adds the lines to a batch write. When courseName changes, the batch is committed, cleared and proceeds to make a new batch for the next students who share the same courseName.

The logic for the batch writing itself looks as follows:

const pathRef = doc(db, "schools/" + userInfoStore.userInfo?.schoolID + '/courses/' + currentCourse + '/students/' + val[columnNames.value.UID])

const data = {
    vorname: val[columnNames.value.Vorname],
    nachname: val[columnNames.value.Nachname],
    klasse: val[columnNames.value.Klasse],
    uid: val[columnNames.value.UID]
}

batch.set(pathRef, data)

This works, however it leaves the collection with the name of "currentcourse" in italic in the database, which means it does not exist itself and can not be found in a firestore query.

How do i prevent this?

答案1

得分: 2

The easiest way is to materialize these non-existing docs (shown in italic) by creating them with a dummy field. Something along the following lines:

const parentPathRef = doc(db, "schools/" + userInfoStore.userInfo?.schoolID + '/courses/' + currentCourse);

const childPathRef = doc(db, "schools/" + userInfoStore.userInfo?.schoolID + '/courses/' + currentCourse + '/students/' + val[columnNames.value.UID]);

const data = {
    vorname: val[columnNames.value.Vorname],
    nachname: val[columnNames.value.Nachname],
    klasse: val[columnNames.value.Klasse],
    uid: val[columnNames.value.UID]
}

batch.set(parentPathRef, { foo: 'bar' });
batch.set(childPathRef, data);
英文:

> This works, however it leaves the collection with the name of
> "currentcourse" in italic in the database, which means it does not
> exist itself and can not be found in firestore queue [I guess you mean "query" here].
>
> How do i prevent this?

The easiest way is to materialize these non-existing docs (shown in italic) by creating them with a dummy field. Something along the following lines:

const parentPathRef = doc(db, "schools/" + userInfoStore.userInfo?.schoolID + '/courses/' + currentCourse);

const childPathRef = doc(db, "schools/" + userInfoStore.userInfo?.schoolID + '/courses/' + currentCourse + '/students/' + val[columnNames.value.UID]);

const data = {
    vorname: val[columnNames.value.Vorname],
    nachname: val[columnNames.value.Nachname],
    klasse: val[columnNames.value.Klasse],
    uid: val[columnNames.value.UID]
}

batch.set(parentPathRef, {foo; 'bar'});
batch.set(childPathRef, data);

huangapple
  • 本文由 发表于 2023年6月15日 00:40:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76475801.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定