简体   繁体   English

ref 不是 function,firebase 存储图片上传,reactJS

[英]ref is not a function, firebase storage image upload, reactJS

Hey guys I keep getting this error, while trying to upload an image to my firebase storage.大家好,我在尝试将图像上传到我的 firebase 存储时不断收到此错误。

Unhandled Rejection (TypeError): firebase_index__WEBPACK_IMPORTED_MODULE_2 _.default.ref is not a function未处理的拒绝(类型错误): firebase_index__WEBPACK_IMPORTED_MODULE_2 _.default.ref 不是 function

This is the component I am trying to upload image from.这是我试图从中上传图像的组件。

import React, { useState, useEffect } from 'react';
import storage from '../firebase/index';

// Imported components
import Section from '../components/Layout/Section';
import Container from '../components/Layout/Container';
import Dropzone from 'react-dropzone';

function FolderProjectUpload() {
  const [file, setFile] = useState(null);
  const [url, setURL] = useState('');

  function handleChange(e, acceptedFiles) {
    setFile(acceptedFiles);
  }

  function handleUpload(e, acceptedFiles) {
    const uploadTask = storage
      .ref(`/images/${acceptedFiles.name}`)
      .put(acceptedFiles);
    uploadTask.on('state_changed', console.log, console.error, () => {
      storage
        .ref('images')
        .child(acceptedFiles.name)
        .getDownloadURL()
        .then((url) => {
          setFile(null);
          setURL(url);
        });
    });
  }

  return (
    <Section justifyContent="center">
      <Container width="1080px">
        <Dropzone onDrop={handleUpload}>
          {({ getRootProps, getInputProps }) => (
            <section>
              <div {...getRootProps()}>
                <input {...getInputProps()} onChange={handleChange} />
                <p>Drag 'n' drop some files here, or click to select files</p>
              </div>
            </section>
          )}
        </Dropzone>
        <Container width="50%" background="white"></Container>
      </Container>
    </Section>
  );
}

export default FolderProjectUpload;

This is how I import storage into firebase configuration:这就是我将存储导入 firebase 配置的方式:

import firebase from 'firebase';
import 'firebase/firestore';
import 'firebase/storage';

firebase.initializeApp({
  apiKey: 'XY',
  authDomain: 'XY',
  databaseURL: 'XY',
  projectId: 'XY',
  storageBucket: 'XY',
  messagingSenderId: 'XY',
  appId: 'XY',
  measurementId: 'XY',
});

let db = firebase.firestore();
let storage = firebase.storage();

export default {
  firebase,
  db,
  storage,
};

Anybody has ideas on what I am doing wrong?有人对我做错了什么有想法吗?

I think your import line is wrong.我认为你的进口线是错误的。 Assuming that the second file you showed is "../firebase/index", your import should be like this, to call out the specific property of the exported object:假设你显示的第二个文件是“../firebase/index”,你的导入应该是这样的,调用导出的object的具体属性:

import { storage } from '../firebase/index';

Or try:或者尝试:

import firebase from '../firebase/index';
let storage = firebase.storage();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM