编译时存在未定义'firebase'问题



好的,所以错误来自第37行,我试图收集每个帖子的时间戳,然后将信息引导到我的firebase存储中。当我导入firebase.js文件时,我不太清楚是什么原因导致了错误,也不知道我是如何得到错误的。

import React, { useState } from 'react';
import { Button } from '@material-ui/core';
import { storage, db } from './firebase';
function ImageUpload({username}) {
const [image, setImage] = useState(null);
const [progress, setProgress] = useState(0);
const [caption, setCaption] = useState('');
const handleChange = (e) => {
if (e.target.files[0]) {
setImage(e.target.files[0]);
}
};
const handleUpload = () => {
const uploadTask = storage.ref('images/${image.name}').put(image);
uploadTask.on(
'state_changed',
(snapshot) => {
const progress = Math.round(
(snapshot.bytesTransferred / snapshot.totalBytes) *100
);
setProgress(progress);
},
(error) => {
console.log(error);
},
() => {
storage
.ref("images")
.child(image.name)
.getDownloadURL()
.then(url => {
db.collection("posts").add({
timestamp: firebase.firestore.FieldValue.serverTimestamp(),
caption: caption,
imageUrl: url,
username: username
})
})
}
)
}
return (
<div>
<input type="text" placeholder='Caption' onChange={event => setCaption(event.target.value)} value={caption}/>
<input type="file" onChange={handleChange} />
<Button className="imageUpload" onClick ={handleUpload}>Upload</Button>
</div>
)

}导出默认ImageUpload

我通过包含行来解决问题

import firebase from 'firebase/compat/app';

插入页首。

最新更新