0

当用户删除主集合时,我正在尝试将所有文​​件从 firebase 存储移动到同一 firebase 存储中的另一个路径,因此出现以下错误: 错误

我尝试并使用了以下功能:

    const restoreDeletedFile = (
    storageRef,
    fileArr,
    personRecordId,
    metadata,
    convertToOriginalPath = false
) => {
    // loop through files metadata
    let restoreFilesPromises = fileArr.map((file, index) => {
        return storageRef
            .child(file.data().path)
            .getDownloadURL()
            .then((url) => {
                // `url` is the download URL for the file
                // This can be downloaded directly:
                var xhr = new XMLHttpRequest();
                xhr.responseType = "blob";
                xhr.onload = () => {
                    let fileToUpload = xhr.response;

                    // upload a file to new path and remove the old one
                    // the new path = pr/id/image.jpeg
                    // the old path = deletedPR/pr/image.jpeg
                    return storageRef
                    // the new path
                    // convertToOriginal is to return filesName in the path as original
                    // ex: deletedPR/pr/fileName__documentId.extention to 
                    // pr/fileName.extention
                        .child(
                            formatFilePath(
                                file.data(),
                                personRecordId,
                                FILE_PATH.ORIGINAL,
                                convertToOriginalPath
                            )
                        )
                        // the old path
                        .put(fileToUpload, metadata[index])
                        .then(() =>
                          // remove old path
                            storageRef.child(file.data().path).delete()
                        );
                };
                xhr.open("GET", url);
                xhr.send();
                return url;
            });
    });
    return Promise.all(restoreFilesPromises);
};

如果我恢复一组文件,则上述功能在一种情况下有效,但如果我删除主集合并尝试使用所有链接的集合恢复已删除的文件,该功能将不起作用,它不会移动文件,我试过了和控制台记录所有代码,但没有错误,一切都符合预期。

当我调用该函数时,下面的代码显示:

                         .then(() =>
                            // fetch person record files
                            firestore
                                .collection("personRecords")
                                .doc("*deleted")
                                .collection("deletedRecords")
                                .doc(personRecordId)
                                .collection("files")
                                .get()
                        )
                        .then((docs) => {
                            fileArr = docs.docs;
                           // it saves the the docuemnet so good(I have checked firestore 
                           // document
                            fileArr.map((doc) => {
                                return firestore
                                    .collection("personRecords")
                                    .doc(personRecordId)
                                    .collection("files")
                                    .doc()
                                    .set({
                                        docSource: doc.data().docSource,
                                        lastModifiedDate:
                                            doc.data().lastModifiedDate,
                                        name: doc.data().name,
                                        path: formatFilePath(
                                            doc.data(),
                                            personRecordId,
                                            FILE_PATH.ORIGINAL
                                        ),
                                        size: doc.data().size,
                                        type: doc.data().type,
                                        uploaderId:
                                            doc.data().uploaderId,
                                    });
                            });
                        })
                        .then(() => {
                            // fetch files metadata
                            let getDeletedMetaDataPromises =
                                fileArr.map((file, index) => {
                                    return storageRef
                                        .child(file.data().path)
                                        .getMetadata();
                                });
                            return Promise.all(
                                getDeletedMetaDataPromises
                            );
                        })
                        .then((metadataSnapshot) => {
                            // metadata to use it later on..
                            metadata = metadataSnapshot.customMetadata;
                            return metadataSnapshot;
                        })
                       
                        .then(() =>
                          // i have console log all the parameters and they are ok, but I 
                           // still see the error above
                            restoreDeletedFile(
                                storageRef,
                                fileArr,
                                personRecordId,
                                metadata
                            )
                        )

有人遇到过这个问题吗?

4

1 回答 1

0

我忘了添加 return Promise.all() ,这就是我遇到该错误的原因

              .then((docs) => {
                        fileArr = docs.docs;
                       // it saves the the docuemnet so good(I have checked 
                        firestore 
                       // document
                      let restorePromise=  fileArr.map((doc) => {
                            return firestore
                                .collection("personRecords")
                                .doc(personRecordId)
                                .collection("files")
                                .doc()
                                .set({
                                    docSource: doc.data().docSource,
                                    lastModifiedDate:
                                        doc.data().lastModifiedDate,
                                    name: doc.data().name,
                                    path: formatFilePath(
                                        doc.data(),
                                        personRecordId,
                                        FILE_PATH.ORIGINAL
                                    ),
                                    size: doc.data().size,
                                    type: doc.data().type,
                                    uploaderId:
                                        doc.data().uploaderId,
                                });
                        });
                  // added promise and the issue has been resolved
               return Promise.all(restorePromise);
                    })
于 2021-09-27T12:15:03.580 回答