Javascript 如何显示从firestore检索到的时间戳类型元素

Javascript 如何显示从firestore检索到的时间戳类型元素,javascript,reactjs,firebase,google-cloud-firestore,Javascript,Reactjs,Firebase,Google Cloud Firestore,我想显示用Firestore创建的收藏的内容,我有一个类型为timestamp的字段“date” 当我想显示这些值时​​我得到一个错误: 错误:对象作为React子对象无效(找到:具有键{秒,纳秒}的对象)。如果要呈现子对象集合,请改用数组 我在@material ui的表格中显示我收藏的内容 index.js import Table from '@material-ui/core/Table'; import TableBody from '@material-ui/core/TableBo

我想显示用Firestore创建的收藏的内容,我有一个类型为timestamp的字段“date”

当我想显示这些值时​​我得到一个错误:

错误:对象作为React子对象无效(找到:具有键{秒,纳秒}的对象)。如果要呈现子对象集合,请改用数组

我在@material ui的表格中显示我收藏的内容

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&nbsp;</TableCell>
                        </TableRow>
                        </TableHead>
                        <TableBody>
                        {nameColl.map(nameColl => (
                            <TableRow key={nameColl.id}>
                            <TableCell align="right"> {nameColl.date} </TableCell> 
                            </TableRow>
                        ))}
                        </TableBody>
                    </Table>
                    </TableContainer>
          )
import app from 'firebase/app';
import 'firebase/firestore'

const config = {...};


class Firebase{

    constructor(){
        app.initializeApp(config)
        this.db = app.firestore();
    }
   
}

export default Firebase;

当您向视图提供Firestore时间戳对象时,您必须弄清楚实际想要显示什么。我认为,在呈现时会出现问题:

<TableCell align="right"> {nameColl.date} </TableCell> 
{nameColl.date}
错误消息提示您不能在此处提供对象。您可能需要某种字符串格式。时间戳对象不会为您格式化自身(它不知道您实际想要什么)。但您可以将时间戳转换为日期,然后将其转换为默认字符串格式:

<TableCell align="right"> {nameColl.date.toDate().toString()} </TableCell> 
{nameColl.date.toDate().toString()}

但是,如果这对您不起作用,您最终还是会弄清楚您真正想要显示什么。

当您向视图提供Firestore时间戳对象时,您必须弄清楚您真正想要显示什么。我认为,在呈现时会出现问题:

<TableCell align="right"> {nameColl.date} </TableCell> 
{nameColl.date}
错误消息提示您不能在此处提供对象。您可能需要某种字符串格式。时间戳对象不会为您格式化自身(它不知道您实际想要什么)。但您可以将时间戳转换为日期,然后将其转换为默认字符串格式:

<TableCell align="right"> {nameColl.date.toDate().toString()} </TableCell> 
{nameColl.date.toDate().toString()}

但是,如果这对您不起作用,您最终还是会找到您真正想要显示的内容。

您好,谢谢您的帮助,我缺少的是toString()。我使用新的Intl.DateTimeFormat(…).format(name.Coll).toString()你好,谢谢你的帮助,我缺少的是toString()。我使用新的Intl.DateTimeFormat(…).format(name.Coll.toString()