2 years ago

#70992

test-img

Jørgen Rasmussen

Cannot upload multiple files when using the firebase emulator

I have an angular web application that can upload multiple files to firebase storage. It works fine with the production server, but when I try to upload using the firebase emulator I get this error if I upload more that 2 files simultaneously (it works fine with 1 og 2 files):

>  /Users/jr/projects/docavea/functions/node_modules/@firebase/database/dist/index.standalone.js:10960
>          throw new Error(errorPrefix +
>                ^
>  Error: transaction failed: Data returned contains NaN in property 'files.referenceCount'
>      at validateFirebaseData (/Users/jr/projects/docavea/functions/node_modules/@firebase/database/dist/index.standalone.js:10960:15)
>      at /Users/jr/projects/docavea/functions/node_modules/@firebase/database/dist/index.standalone.js:11001:13
>      at each (/Users/jr/projects/docavea/functions/node_modules/@firebase/database/dist/index.standalone.js:525:13)
>      at validateFirebaseData (/Users/jr/projects/docavea/functions/node_modules/@firebase/database/dist/index.standalone.js:10984:9)
>      at _loop_2 (/Users/jr/projects/docavea/functions/node_modules/@firebase/database/dist/index.standalone.js:11977:21)
>      at repoRerunTransactionQueue (/Users/jr/projects/docavea/functions/node_modules/@firebase/database/dist/index.standalone.js:12030:9)
>      at repoRerunTransactions (/Users/jr/projects/docavea/functions/node_modules/@firebase/database/dist/index.standalone.js:11929:5)
>      at /Users/jr/projects/docavea/functions/node_modules/@firebase/database/dist/index.standalone.js:11910:13
>      at /Users/jr/projects/docavea/functions/node_modules/@firebase/database/dist/index.standalone.js:3711:17
>      at PersistentConnection.onDataMessage_ (/Users/jr/projects/docavea/functions/node_modules/@firebase/database/dist/index.standalone.js:3738:17)
>      at Connection.onDataMessage_ (/Users/jr/projects/docavea/functions/node_modules/@firebase/database/dist/index.standalone.js:2532:14)
>      at Connection.onPrimaryMessageReceived_ (/Users/jr/projects/docavea/functions/node_modules/@firebase/database/dist/index.standalone.js:2526:18)
>      at WebSocketConnection.onMessage (/Users/jr/projects/docavea/functions/node_modules/@firebase/database/dist/index.standalone.js:2428:27)
>      at WebSocketConnection.appendFrame_ (/Users/jr/projects/docavea/functions/node_modules/@firebase/database/dist/index.standalone.js:1141:18)
>      at WebSocketConnection.handleIncomingFrame (/Users/jr/projects/docavea/functions/node_modules/@firebase/database/dist/index.standalone.js:1189:22)
>      at Client.mySock.onmessage (/Users/jr/projects/docavea/functions/node_modules/@firebase/database/dist/index.standalone.js:1088:19)

My code to upload:

private pushFile(storageReference: firebase.storage.Reference, upload: Upload, uid: string) {

    // set metadata when writing file so only the uploading user have read access to get the downloadURL
    const metadata = {
        customMetadata: {
            uid
        }
    }
    const uploadTask = storageReference.put(upload.file, metadata);

    const startUploadProgress = this.uploadProgress;
    let progressLastUpdated = 0;
    uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED,
        (snapshot: any) => {
            // upload in progress
            // due to the way mat progress bar is made it causes flicker to update more often than every 250ms
            // using 1000ms seems to make it fairly smooth
            // https://github.com/angular/components/issues/15138
            if (Date.now() - progressLastUpdated > 1000) {
                progressLastUpdated = Date.now();
                upload.progress = snapshot.bytesTransferred;
                this.uploadBytesTransferred = 0;
                this.uploads.forEach(upl => this.uploadBytesTransferred += upl.progress)
                this.uploadProgress = startUploadProgress + (this.uploadBytesTransferred / this.uploadTotalSize) * (100 - startUploadProgress);
            }
        },
        (error) => {
            // upload failed
            console.error(error);
        },
        () => {
            // console.log('storage.service.ts:pushFile() Upload complete');
        }
    );
        return uploadTask;
    }

Is this a limitation in the storage emulator, or am I doing something wrong?

javascript

firebase-storage

firebase-tools

0 Answers

Your Answer

Accepted video resources