我想显示用Firestore创建的集合的内容,我有一个字段"date“,它的类型是时间戳。
当我想显示值时,我得到一个错误:
错误:对象作为React子对象无效(找到:键为{秒的对象,纳秒})。如果您打算呈现一个子集合,请使用数组代替。
我将集合的内容显示在@material中的一个表中。
index.js
import Table from '@material-ui/core/Table';
import TableBody from '@material-ui/core/TableBody';
import TableCell from '@material-ui/core/TableCell';
import TableContainer from '@material-ui/core/TableContainer';
import TableHead from '@material-ui/core/TableHead';
import TableRow from '@material-ui/core/TableRow';
import Paper from '@material-ui/core/Paper';
import { FirebaseContext } from '../Firebase'
const Welcome = () => {
const firebase = useContext(FirebaseContext);
const [nameColl, setNameColl] = React.useState([]);
React.useEffect(() => {
const fetchData = async () => {
const db = firebase.db;
const data = await db.collection("nameColl").get();
setNameColl(data.docs.map(doc => ({ ...doc.data(), id: doc.id })));
};
fetchData();
}, []);
return(
<TableContainer component={Paper}>
<Table aria-label="simple table">
<TableHead>
<TableRow>
<TableCell align="right"> Date </TableCell>
</TableRow>
</TableHead>
<TableBody>
{nameColl.map(nameColl => (
<TableRow key={nameColl.id}>
<TableCell align="right"> {nameColl.date} </TableCell>
</TableRow>
))}
</TableBody>
</Table>
</TableContainer>
)
firebase.js
import app from 'firebase/app';
import 'firebase/firestore'
const config = {...};
class Firebase{
constructor(){
app.initializeApp(config)
this.db = app.firestore();
}
}
export default Firebase;
发布于 2020-08-13 20:21:55
当您向视图提供Fi还原时间戳对象时,您必须弄清楚实际想要显示什么。我想,这个问题是在这样做时发生的:
<TableCell align="right"> {nameColl.date} </TableCell>
错误消息表明您不能在这里提供一个对象。您可能需要某种类型的字符串格式。时间戳对象不会为您设置自己的格式(它不知道您实际需要什么)。但是您可以将时间戳转换为日期,然后将其转换为默认的字符串格式:
<TableCell align="right"> {nameColl.date.toDate().toString()} </TableCell>
但你最终还是会发现你真正想要的是什么,如果这对你不起作用的话。
https://stackoverflow.com/questions/63402583
复制相似问题