代码之家  ›  专栏  ›  技术社区  ›  Darren

storageref.child不是firebase中的函数

  •  1
  • Darren  · 技术社区  · 7 年前

    在使用 react-firebase-file-uploader 包裹。

    收到的错误是 Uncaught (in promise) TypeError: storageRef.child is not a function

    首先, firebase 是从我的盖茨比家族进口的 Layout.js 在里面 componentDidMount 作为

    布局图

    componentDidMount() {
      const app = import('firebase/app');
      const auth = import('firebase/auth');
      const database = import('firebase/firestore');
      const storage = import('firebase/storage');
    
      Promise.all([app, auth, database, storage]).then(values => {
        const firebase = getFirebase(values[0]);
        !this.isCancelled && this.setState({ firebase });
      });
    }
    

    然后, 火力基地 添加为 props 使用 React.createContext 通过 functions 组件组件组件

    firebasecontext.js文件

    const FirebaseContext = React.createContext(null);
    export const withFirebase = Component => props => (
      <FirebaseContext.Consumer>
        {firebase => <Component {...props} firebase={firebase} />}
      </FirebaseContext.Consumer>
    );
    
    export default FirebaseContext;
    

    里面 火力基地 组件,我们使用 onboardStorage 作为包含 this.storage 作为

    FiasBaseJS

    class Firebase {
      constructor(app) {
        app.initializeApp(config);
    
        /* Firebase APIs */
        this.app = app;
        this.storage = app.storage();
    
      // *** Storage API ***
      onboardStorage = () => this.storage;
    }
    
    let firebase;
    
    function getFirebase(app, auth, database, storage) {
      if (!firebase) {
        firebase = new Firebase(app, auth, database, storage);
      }
    
      return firebase;
    }
    
    export default getFirebase;
    

    然后传递给 FileUploader 组件在我的窗体中组件为

    JS

    <FileUploader
      accept="image/*"
      name="avatar"
      filename="operator_accreditation"
      storageRef={() => firebase.onboardStorage().ref(`${uid}/files`)}
      onUploadStart={this.handleUploadStart}
      onUploadError={this.handleUploadError}
      onUploadSuccess={this.handleUploadSuccess}
      onProgress={this.handleProgress}
    />
    

    中的每个句柄函数 JS 如下所示

        handleUploadStart = () =>
        this.setState({
          operatorAccreditationIsUploading: true,
          operatorAccreditationProgress: 0
        });
    
      handleProgress = operatorAccreditationProgress =>
        this.setState({ operatorAccreditationProgress });
    
      handleUploadError = error => {
        this.setState({ operatorAccreditationIsUploading: false });
        console.error(error);
      };
    
      handleUploadSuccess = filename => {
        const { firebase, uid } = this.props;
        this.setState({
          operatorAccreditation: filename,
          operatorAccreditationProgress: 100,
          operatorAccreditationIsUploading: false
        });
        const storageOperatorAccreditation = firebase.onboardStorage();
        storageOperatorAccreditation
          .ref(`${uid}/files`)
          .child(filename)
          .getDownloadURL()
          .then(url => this.setState({ operatorAccreditation: url }));
      };
    

    const uid auth.uid

    添加一个 console 消息指示上载文件时不会触发任何操作。

    选择文件后发生错误,表明问题在 storageRef 文件复制器 . 我做错了什么 uncaught(in promise)类型错误:storageref.child不是函数 ?

    0 回复  |  直到 7 年前
        1
  •  1
  •   leiropi    7 年前

    我也有类似的问题。问题是您将函数作为storageref属性传递给fileuploader。相反,您应该像这样直接传递一个StorageRef对象:

    <FileUploader
      accept="image/*"
      name="avatar"
      filename="operator_accreditation"
      storageRef={firebase.onboardStorage().ref(`${uid}/files`)}
      onUploadStart={this.handleUploadStart}
      onUploadError={this.handleUploadError}
      onUploadSuccess={this.handleUploadSuccess}
      onProgress={this.handleProgress}
    />
    
    推荐文章