无法从Firebase存储产生可下载的链接

时间:2019-04-10 11:16:40

标签: android firebase react-native firebase-storage

我正在开发一个React本机应用程序。我正在使用react-native-image-crop-picker上传一批图像。使用将路径存储在数组中并一一上传图像的方式。但我无法生成上传文件的可下载链接。我遇到2个错误

  

1)无法读取未定义的属性“ then”
  2)尝试获取可下载链接时,找不到对象

下面是我的代码

            AddProduct = () => {
                this.setState({ loading: true })
                this.setState({ disabled: true })
                //-------------UUIDGenerator-------------

                UUIDGenerator.getRandomUUID().then((productUid) => {
                this.setState({ productUid });
                () => console.log("setted state=" + this.state.productUid)
                console.log("Generated UUid " + this.state.productUid)
                });
                //----------------------------------------
                //image upload
                try {

                setTimeout(() => {
                    const fs = RNFetchBlob.fs
                    const uid = "flyers/" + this.state.productUid;
                    const imageRef = firebase.storage().ref(uid).child('abc' + ".jpg") //string "dp1.jpg"
                    let mime = 'image/jpg'

                    if (this.state.imagePath === "") {
                    alert("Select a Flyer")
                    return
                    }
                    //-----------------Checking Inputs-----------
                    if (this.state.title.length < 1) {
                    alert("Enter a Title");
                    return;
                    }

                    if (this.state.description.length < 3) {
                    alert("Enter At least 3 words To describe the product ")
                    return;
                    }
                    if (this.state.storename.length < 1) {
                    alert("Enter a Storename")
                    return;
                    }
                    alert("Uploading Flyer" + this.state.title)
                    //------------------------------------------------
                    this.state.imagePath.forEach((data) => {

                    // coverting to base64
                    new Promise((resolve, reject) => {
                    fs.readFile(data, 'base64')
                    .then((data) => {

                        return Blob.build(data, { type: `${mime};BASE64` })

                    })
                    .then((blob) => { 
                        console.log("blob " +JSON.stringify(blob))
                        //uplaoding Image
                        uploadBlob = blob
                        name =name + 1
                        firebase.storage().ref(uid).child(name + ".jpg").put(blob) // ,{ contentType: mime }

                    })
                    .then(() => {
                        //uploadBlob.close()
                        //getting url
                        console.log("1")
                        return firebase.storage().ref(uid).child(name + ".jpg").getDownloadURL()

                    }) 
                    })
                    })
                    .then(() => {
                        //urls = url;
                        console.log('urls=' + urls)
                        //================================
                        try {


                        alert("Uploading Flyerss" + this.state.title)

                        //-------------------------------------------

                        //----------Inserting Data to Database--------


                        usersTable = 'flyers/' + this.state.productUid,
                            console.log(usersTable)
                        firebase.database().ref(usersTable).set(
                            {

                            title: this.state.title,
                            description: this.state.description,
                            imageUrls: this.state.imageUrls,
                            storename: this.state.storename,
                            user: asyncValue,
                            longitude: this.state.longitude,
                            latitutde: this.state.latitutde
                            }
                        ).then(
                            console.log("then"),
                            this.title.clear(),
                            this.description.clear(),
                            //this.category.clear(),
                            this.setState({ loading: false }),
                            this.setState({ disabled: false }),
                            this.setState({ title: "" }),
                            this.setState({ storename: "" }),
                            this.setState({ description: "" }),
                            this.setState({ imagePath: "" }),
                            urls = "",
                            alert(this.state.title + " flyer sucessfully uploaded")
                        )
                        //--------------------------------------------

                        }
                        catch (error) {
                        this.setState({ loading: false })
                        this.setState({ disabled: false })
                        console.log(error.toString())
                        alert(error.toString())
                        }
                        //================================



                    })
                }, 3000)
                }

                catch (error) {
                console.log("upload Error = " + error)
                }

            }

0 个答案:

没有答案