1
votes

Rightnow i'm using a array.map method to upload each file and gets the url in response but the thing is it gives me "C:\fakepath\pic1" in some files which creates a messs although it uploads it correctly(means i've seen the images in my firebase storage) but not get back the url in response
This is the code i'm using to put files

uploadPicsArr.map(function (pic, index) {
                var storageRef = fbStorage.ref('images/'+token+'/'+pic.name);
                // Upload file to Firebase Storage
                var uploadTask = storageRef.put(pic.file);
                uploadTask.on('state_changed', null, null, function () {
                    var downloadUrl = uploadTask.snapshot.downloadURL;
                    userInfo[pic.name] = downloadUrl;
                })
                if (uploadPicsArr.length == index + 1) {
                   fbDatabase.ref('users').push(userInfo).then(function (success){
                        browserHistory.push('/home');
                    });
                }
            })

fbStorage: firebase.storage()
uploadPicsArr = an array of objects and every object has a property name and file
name: name of the selected file
file: selected file
Please tell me what i'm doing wrong or if there's any other better way to upload the whole array and get each file URL in response then it would be better

1

1 Answers

0
votes

I think you forget to pass the snapshot as a parameter in the complete function:

uploadPicsArr.map(function (pic, index) {
    var storageRef = fbStorage.ref('images/'+token+'/'+pic.name);
    // Upload file to Firebase Storage
    var uploadTask = storageRef.put(pic.file);
    uploadTask.on('state_changed', null, null, function (snapshot) {
        userInfo[pic.name] = snapshot.downloadURL;
    })
    if (uploadPicsArr.length == index + 1) {
       fbDatabase.ref('users').push(userInfo).then(function (success){
            browserHistory.push('/home');
        });
    }
})