亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Firebase Firestore 僅在第一次嘗試全新構建時寫入

Firebase Firestore 僅在第一次嘗試全新構建時寫入

炎炎設計 2023-09-07 16:33:26
我一直在使用 Firebase 和 React Native 構建一個應用程序,主要使用 Firestore。我開始使用 Firestore,它非常棒,但由于某種原因,在寫入 Firestore 時,它僅在第一次嘗試時有效(當我刪除應用程序、重建并執行寫入時)。我嘗試做完全相同的事情,除了寫入 Firestore 之外,一切都按預期進行。我也沒有收到任何錯誤!這是我正在做的事情:export const addBrandProduct = (postObj) => {  return () => {    firebase      .firestore()      .collection('brandProducts')      .add(postObj)      .then((docRef) => {        console.log("Document written with ID: ", docRef.id);        Actions.categories();      })      .catch(error => {        console.error("Error adding document: ", error);      });  };};如需更多參考,這是我調用 addBrandProduct() 的組件代碼  onUploadImages = () => {    let photo =      Platform.OS === 'ios'        ? this.state.images.map(img => img.uri.replace('file://', ''))        : this.state.images.map(img => img.uri);      photo.forEach((image, i) => {      const sessionId = new Date().getTime();      const Blob = RNFetchBlob.polyfill.Blob;      const fs = RNFetchBlob.fs;      window.XMLHttpRequest = RNFetchBlob.polyfill.XMLHttpRequest;      window.Blob = Blob;      let uploadBlob = null;      let mime = 'image/jpg';      const imageRef = firebase        .storage()        .ref('brandProducts/')        .child(`${this.props.userData.uid}`)        .child(`${sessionId}-${i}`);      fs.readFile(image, 'base64')        .then(data => {          return Blob.build(data, {type: `${mime};BASE64`});        })        .then(blob => {          uploadBlob = blob;          return imageRef.put(blob, {contentType: mime});        })        .then(() => {          uploadBlob.close();          return imageRef.getDownloadURL();        })基本上,我最多上傳 3 張圖像及其一些數據。為了確保我在添加帖子數據(寫入 firestore)之前上傳所有內容,我使用了 forEach ,在最后一次上傳完成后,我調用操作來寫入帖子數據。
查看完整描述

1 回答

?
HUX布斯

TA貢獻1876條經驗 獲得超6個贊

編輯


HumaddBrandProduct是一個創建另一個函數的函數。因此,當您調用this.props.addBrandProduct(postObj)任何內容時,都不會發送到 firestore,您只需創建一個應該調用的新函數。


也許你可以出去這個東西并直接調用 firebase,確保一切正常,然后如果你仍然想使用它,則返回到 redux 方式。我還使其并行化而不是順序化。希望它能有所幫助,當它來自任何地方時很難找到真正的問題。


onUploadImages = () => {

    let photo = Platform.OS === 'ios'

    ? this.state.images.map(img => img.uri.replace('file://', ''))

    : this.state.images.map(img => img.uri);


    Promise.all( photo.map( image => {

        const sessionId = new Date().getTime();

        const Blob = RNFetchBlob.polyfill.Blob;

        //This is kind useless

        //const fs = RNFetchBlob.fs;

        //This is not used

        //window.XMLHttpRequest = RNFetchBlob.polyfill.XMLHttpRequest;

        //This is not adviced

        //window.Blob = Blob;

        let uploadBlob = null;

        let mime = 'image/jpg';


        const imageRef = firebase

        .storage()

        .ref('brandProducts/')

        .child(`${this.props.userData.uid}`)

        .child(`${sessionId}-${i}`);


        return fs.readFile(image, 'base64')

        .then(data => {

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

        })

        .then(blob => {

            uploadBlob = blob;

            return imageRef.put(blob, {contentType: mime});

        })

        .then(() => {

            uploadBlob.close();

            return imageRef.getDownloadURL();

        });

    ))

    .then( results => {

        //results is, here, [ urlFromFirst, urlFronSecond, ...]

        const urls = { ...this.state.urls};

        results.forEach( (r, i) => urls[i] = r );

        const postObj = {

            ...this.state.postObj,

            urls

        };

        

        return firebase

        .firestore()

        .collection('brandProducts')

        .add(postObj)

    })

    .then( docRef => {

        console.log("Document written with ID: ", docRef.id);

    })

    .catch(error => {

        console.error(error);

    });

};



查看完整回答
反對 回復 2023-09-07
  • 1 回答
  • 0 關注
  • 106 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號