【问题标题】:TypeError: (intermediate value).toDate is not a functionTypeError:(中间值).toDate 不是函数
【发布时间】:2021-01-13 05:23:26
【问题描述】:

我正在尝试显示我的 firestore 集合中的时间戳值,但我收到此错误“TypeError: (intermediate value).toDate is not a function”,我在下面用“>>”标记。 我在将其提交到 Firestore 之前将其设置为时间戳,但现在要显示它,我需要将其作为日期值 你有什么解决办法吗?

    componentDidMount() {
        db.collection("mycollection")
          .get()
          .then(querySnapshot => {
            const data = querySnapshot.docs.map(doc => doc.data());
            this.setState({ costumers: data });
          });
      }
    
    
      render(){
        const { costumers } = this.state;
        
    
      return (
        <Card
        
        >
          <PerfectScrollbar>
         
            <Box minWidth={1050}>
          
              <Table   >
                <TableHead>
                  <TableRow>
                    <TableCell padding="checkbox">
                    
                    </TableCell>
                  
                {costumers.map(costumer => (
                <TableBody>
                    <TableRow            
                    >
                      <TableCell padding="checkbox">
                        <Checkbox
                          value="true"
                        />
                      </TableCell>
                      <TableCell>
                        <Box
                          alignItems="center"
                          display="flex"
                        >
                          <Typography
                            color="textPrimary"
                            variant="body1"
                          >
                             {costumer.name}
                          </Typography>
                        </Box>
                      </TableCell>
                      <TableCell>
                      {costumer.surname}
                      </TableCell>
                      <TableCell>
                      {costumer.email}
                      </TableCell>
                      <TableCell>
                      {costumer.phone}
                      </TableCell>
                      <TableCell>
>>                    {new Date(costumer.date).toDate()}   
                      </TableCell>
                      <TableCell>
                     {costumer.time}        
                      </TableCell>
                      <TableCell>
                      {costumer.service}
                      </TableCell>              
                    </TableRow>             
                </TableBody>
                ))}
              </Table>

【问题讨论】:

  • 您是否尝试过 console.log 记录costumer.date 的值?您是否也在为穿着服装的人或客户制作应用程序? ;)

标签: json reactjs firebase google-cloud-firestore timestamp


【解决方案1】:

您可以使用以下代码轻松地从 Firebase Server 本身获取时间戳。

import firebase from 'firebase';

var TimeStamp = firebase.firestore.FieldValue.serverTimestamp();

serverTimestamp() 函数将从 firebase 服务器 本身返回时间戳。在这里,TimeStamp 的值将采用以下格式:

<Full name of the month> dd, yyyy hh:mm:ss <Time zone of your location>

如果要显示日期,只需将其显示为字符串即可。您不能在 &lt;TableCell&gt; 中显示日期值。您必须将其转换为字符串。

编辑 当您收到时间戳值时,它已经是日期格式。您必须使用toDateString() 将其转换为字符串。

【讨论】:

    猜你喜欢
    • 2018-02-10
    • 2022-11-04
    • 2015-07-19
    • 2019-07-06
    • 2018-04-02
    • 2023-01-05
    • 2016-11-06
    • 1970-01-01
    • 2020-01-05
    相关资源
    最近更新 更多