第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

Firebase Firestore 僅在第一次嘗試全新構(gòu)建時寫入

Firebase Firestore 僅在第一次嘗試全新構(gòu)建時寫入

炎炎設(shè)計 2023-09-07 16:33:26
我一直在使用 Firebase 和 React Native 構(gòu)建一個應(yīng)用程序,主要使用 Firestore。我開始使用 Firestore,它非常棒,但由于某種原因,在寫入 Firestore 時,它僅在第一次嘗試時有效(當(dāng)我刪除應(yīng)用程序、重建并執(zhí)行寫入時)。我嘗試做完全相同的事情,除了寫入 Firestore 之外,一切都按預(yù)期進(jìn)行。我也沒有收到任何錯誤!這是我正在做的事情: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);      });  };};如需更多參考,這是我調(diào)用 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 張圖像及其一些數(shù)據(jù)。為了確保我在添加帖子數(shù)據(jù)(寫入 firestore)之前上傳所有內(nèi)容,我使用了 forEach ,在最后一次上傳完成后,我調(diào)用操作來寫入帖子數(shù)據(jù)。
查看完整描述

1 回答

?
HUX布斯

TA貢獻(xiàn)1876條經(jīng)驗 獲得超6個贊

編輯


HumaddBrandProduct是一個創(chuàng)建另一個函數(shù)的函數(shù)。因此,當(dāng)您調(diào)用this.props.addBrandProduct(postObj)任何內(nèi)容時,都不會發(fā)送到 firestore,您只需創(chuàng)建一個應(yīng)該調(diào)用的新函數(shù)。


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


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);

    });

};



查看完整回答
反對 回復(fù) 2023-09-07
  • 1 回答
  • 0 關(guān)注
  • 105 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號