Firestore数组未存储最后一个值

时间:2019-07-04 03:45:27

标签: reactjs google-cloud-firestore

我有一个注册表单,其中包含许多要提交的字段。表单提交工作正常,将所有详细信息保存到了Firestore。我有输入文件上传,用户可以选择多个文件,提交后将所有文件上传到Firebase存储中,然后将上传链接保存在数组(FileURLs)中。

例如,如果我在执行console.log状态日志时选择了3个提交文件,则可以看到包含3个数据的FileURL,但是在firestore中,最后一个值未保存。

代码

handleSubmit = (e) => {
        e.preventDefault();
        const promises = [];
        const {fileURLs,sp_License}=this.state;
        let files=[];
        let orgFile='';
        let err = this.validate();
        if (!err) {
          this.setState({ loading: true,disChecked:false })
        //   this.fileupload();
        //   this.MultifileuploadHandler();
        const Lfilename = this.state.sp_Name + '_' + new Date().getTime();
        const uploadTask = storage.ref('License/' + Lfilename).put(sp_License);
        promises.push(uploadTask);
        uploadTask
        .then(uploadTaskSnapshot => {
            return uploadTaskSnapshot.ref.getDownloadURL();
        })
        .then(url => {
          //  orgFile.push({url});
            // this.setState({sp_License:url})
             orgFile=url
          //  console.log(orgFile)
        },()=>{
            console.log(sp_License)
        });

         //multi pilots file
         const storageRef = storage.ref();
         this.state.sp_PilotsLicense.forEach((file) => {
             const uploadTask= storageRef
                 .child(`License/${file.name}`).put(file)
                 promises.push(uploadTask);
                 uploadTask.then((snapshot) => {
                     return snapshot.ref.getDownloadURL();
                 }).then(url =>{
                     files.push({url});

                 })
         });

         Promise.all(promises).then(tasks => {
            console.log('all uploads complete');
            console.log(this.state)
             if(this._mounted)
            {
                console.log(orgFile)
                 this.setState({ 
                    fileURLs: files,
                    sp_License:orgFile,
                },()=>{
                    console.log(this.state)
                });
            }
            this.saveData();
        });


        }
    }
 saveData=()=>{
        let uid = this.props.auth.uid;
        let keysToRemove = ["loading", "checked", "disChecked", "open", "message"]
       let newState = Object.entries({...this.state}).reduce((obj, [key, value]) => {
         if(!keysToRemove.includes(key)){
             obj[key] = value
         }
         return obj
        }, {})
       console.log(newState)
        this.props.UpdateUserDetails(uid, newState,this.successMessage)
    }

动作

export const UpdateUserDetails= (id, droneSPDetails,func) => {
    console.log(droneSPDetails)
    console.log(func)
    return (dispatch, getState, { getFirestore }) => {
        const firestore = getFirestore()
        firestore.collection('users')
            .doc(id)
            .set({
                 ...droneSPDetails,
                sp_RegisteredOn: new Date(),
                sp_Status:"pending",
                sp_ActiveFlag:"1",
            },{ merge: true })
            .then(() => {
                func();
                dispatch({ type: 'CREATE_DRONESP', droneSPDetails });

            })
            .catch((error) => {
                console.error("Error adding document: ", error);
            });
    }
}

这是传递给存储在firestore上的值(来自console.log)

fileURLs: Array(3)
0: {url: "https://firebasestorage.googleapis.com/v0/b/dronew…=media&token=084bc59a-8087-43de-afdb-05d4192ec4d2"}
1: {url: "https://firebasestorage.googleapis.com/v0/b/dronew…=media&token=3d427621-6a8b-4ed7-b6ce-5c3940fe7ee6"}
2: {url: "https://firebasestorage.googleapis.com/v0/b/dronew…=media&token=e24a5bab-e584-4a95-bb70-4d6c9f7fed7c"}
length: 3

但是我只能看到这两个值被保存。

firebase data

0 个答案:

没有答案