这种方法似乎破坏了我的本机代码

时间:2019-06-19 05:55:43

标签: javascript react-native google-cloud-firestore firebase-storage

我正在尝试将图像从我的本机应用程序上载到Firebase云存储。上载实际上是成功的,我在下面发布的方法有效。问题是,此方法以某种方式破坏了我的应用程序,因此我无法写入Firestore 数据库中的集合。我怎么知道因为当我禁用对此功能的调用时,可以调用其他“ SAVE”方法,因此我可以将文档记录到Firestore集合中。仅当执行此方法并且我将图像上载到firebase 存储,然后停止写入firestore 数据库集合时,才会停止。我想知道RNFetchBlob是否有问题?

在此,我将不胜感激。

谢谢!

uploadImage(uri, mime, name) {
        console.log("inside uploadImage function");
        try{
            const Blob = RNFetchBlob.polyfill.Blob;
            const fs = RNFetchBlob.fs;
            window.XMLHttpRequest = RNFetchBlob.polyfill.XMLHttpRequest;
            window.Blob = Blob;  
            var imgUri = uri; 
            var uploadBlob = null;
            const uploadUri = Platform.OS === 'ios' ? imgUri.replace('file://', '') : imgUri;
            var imageKey = (this.state.currentImageIndex).toString();//this.guidGenerator();
            var imageRef = firebase.storage().ref(`/images/${imageKey}`);

            fs.readFile(uploadUri, 'base64')
            .then(data => {
                return Blob.build(data, { type: `${mime};BASE64` });
            })
            .then(blob => {
                uploadBlob = blob;
                var uploadTask = imageRef.put(blob, { contentType: mime, name: name });
                uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED, // or 'state_changed'
                    (snapshot) => {
                        // Get task progress, including the number of bytes uploaded and the total number of bytes to be uploaded
                        var progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
                        console.log('Upload is ' + progress + '% done');
                        switch (snapshot.state) {
                        case firebase.storage.TaskState.PAUSED: // or 'paused'
                            console.log('Upload is paused');
                            break;
                        case firebase.storage.TaskState.RUNNING: // or 'running'
                            console.log('Upload is running');
                            break;
                        }
                    }, (error) => {
                        console.log(error);
                    // A full list of error codes is available at
                    // https://firebase.google.com/docs/storage/web/handle-errors
                    switch (error.code) {
                        case 'storage/unauthorized':
                        // User doesn't have permission to access the object
                        break;

                        case 'storage/canceled':
                        // User canceled the upload
                        break;

                        case 'storage/unknown':
                        // Unknown error occurred, inspect error.serverResponse
                        break;
                    }
                    }, () => {
                        // Upload completed successfully, now we can get the download URL
                        uploadTask.snapshot.ref.getDownloadURL().then((downloadURL)=> {
                            try{
                                console.log('File available at', downloadURL);

                                uploadBlob.close();
                                fs.unlink(uploadUri);
                                console.log("blob closed");

                                this.setState({currentImageIndex:this.state.currentImageIndex+1}, ()=>{
                                    if(this.state.pickType ==='multi'){
                                        if(this.state.currentImageIndex<this.state.totalNumImages){
                                            console.log("call openCropper");
                                            this.openCropper();
                                        }else{
                                            this.setState({totalNumImages:0});
                                            this.setState({currentImageIndex:0});
                                        }
                                    }
                                });
                            }catch(err){
                                console.log(err);
                            }
                        }).catch((err)=>{
                            console.log(err);
                        });
                    });
            })
            .catch((err)=>{
                console.log("ERROR READING FILE: fs.readFile!!");
                console.log(err);
            });
        }
        catch(err){
            console.log(err);
        }
    }

0 个答案:

没有答案