为了账号安全,请及时绑定邮箱和手机立即绑定

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 关注
  • 54 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信