【发布时间】:2020-04-04 06:24:49
【问题描述】:
我正在尝试弄清楚如何在 react 应用中显示 firestore 时间戳。
我有一个带有名为 createdAt 的字段的 firestore 文档。
我正在尝试将其包含在输出列表中(在此处提取相关位,这样您就不必通读整个字段列表)。
componentDidMount() {
this.setState({ loading: true });
this.unsubscribe = this.props.firebase
.users()
.onSnapshot(snapshot => {
let users = [];
snapshot.forEach(doc =>
users.push({ ...doc.data(), uid: doc.id }),
);
this.setState({
users,
loading: false,
});
});
}
componentWillUnmount() {
this.unsubscribe();
}
render() {
const { users, loading } = this.state;
return (
<div>
{loading && <div>Loading ...</div>}
{users.map(user => (
<Paragraph key={user.uid}>
<key={user.uid}>
{user.email}
{user.name}
{user.createdAt.toDate()}
{user.createdAt.toDate}
{user.createdAt.toDate()).toDateString()}
唯一不会呈现的属性是日期。
上述每一次尝试都会产生一个错误,上面写着:
TypeError: 无法读取未定义的属性“toDate”
我见过this post、this post、this post、this post 和其他类似的,这表明 toDate() 应该可以工作。但是 - 这个扩展对我来说是一个错误 - 包括当我尝试 toString 扩展时。
我知道它知道 firestore 中有东西,因为当我尝试 user.createdAt 时,我收到一条错误消息,说它找到了一个包含秒数的对象。
以下面的Waelmas为例,我尝试将字段的输出记录为:
this.db.collection('users').doc('HnH5TeCU1lUjeTqAYJ34ycjt78w22').get().then(function(doc) {
console.log(doc.data().createdAt.toDate());
}
我也尝试将此添加到我的 map 语句中,但收到一条错误消息,提示 user.get 不是函数。
{user.get().then(function(doc) {
console.log(doc.data().createdAt.toDate());}
)}
它会生成与上面相同的错误消息。
下一次尝试
在试图找到一种方法在 Firestore 中记录日期以允许我读取它时出现了一件奇怪的事情,那就是当我以一种形式更改我的提交处理程序以使用此公式时:
handleCreate = (event) => {
const { form } = this.formRef.props;
form.validateFields((err, values) => {
if (err) {
return;
};
const payload = {
name: values.name,
// createdAt: this.fieldValue.Timestamp()
// createdAt: this.props.firebase.fieldValue.serverTimestamp()
}
console.log("formvalues", payload);
// console.log(_firebase.fieldValue.serverTimestamp());
this.props.firebase
.doCreateUserWithEmailAndPassword(values.email, values.password)
.then(authUser => {
return this.props.firebase.user(authUser.user.uid).set(
{
name: values.name,
email: values.email,
createdAt: new Date()
// .toISOString()
// createdAt: this.props.firebase.fieldValue.serverTimestamp()
},
{ merge: true },
);
// console.log(this.props.firebase.fieldValue.serverTimestamp())
})
.then(() => {
return this.props.firebase.doSendEmailVerification();
})
// .then(() => {message.success("Success") })
.then(() => {
this.setState({ ...initialValues });
this.props.history.push(ROUTES.DASHBOARD);
})
});
event.preventDefault();
};
这可以在数据库中记录日期。
firestore 条目的形式如下所示:
我正在尝试在此组件中显示日期:
class UserList extends Component {
constructor(props) {
super(props);
this.state = {
loading: false,
users: [],
};
}
componentDidMount() {
this.setState({ loading: true });
this.unsubscribe = this.props.firebase
.users()
.onSnapshot(snapshot => {
let users = [];
snapshot.forEach(doc =>
users.push({ ...doc.data(), uid: doc.id }),
);
this.setState({
users,
loading: false,
});
});
}
componentWillUnmount() {
this.unsubscribe();
}
render() {
const { users, loading } = this.state;
return (
<div>
{loading && <div>Loading ...</div>}
<List
itemLayout="horizontal"
dataSource={users}
renderItem={item => (
<List.Item key={item.uid}>
<List.Item.Meta
title={item.name}
description={item.organisation}
/>
{item.email}
{item.createdAt}
{item.createdAt.toDate()}
{item.createdAt.toDate().toISOString()}
</List.Item>
// )
)}
/>
</div>
);
}
}
export default withFirebase(UserList);
当我尝试回读时 - 使用:
{item.email}
错误信息如下:
错误:对象作为 React 子对象无效(发现: 时间戳(秒=1576363035,纳秒=52000000))。如果你打算 渲染一组孩子,请改用数组。 在项目中(在 UserIndex.jsx:74)
当我尝试使用这些尝试时:
{item.createdAt}
{item.createdAt.toDate()}
{item.createdAt.toDate().toISOString()}
我收到一条错误消息:
TypeError: 无法读取未定义的属性“toDate”
基于在其他字段中回读记录在同一文档中的条目的能力,我希望这些条目中的任何一个都能产生输出——即使它没有按照我想要的方式格式化。这不会发生。
下一次尝试
以 Waelmas 为例,我尝试按照说明进行操作,但第一步是我们没有得到相同的响应。在 Walemas 获得基于 .toDate() 扩展名的输出的地方,我收到一条错误消息,指出 toDate() 不是函数。
与 Firebase 文档一致,我尝试过:
const docRef = this.props.firebase.db.collection("users").doc("HnH5TeCU1lUjeTqAYJ34ycjt78w22");
docRef.get().then(function(docRef) {
if (doc.exists) {
console.log("Document createdAt:", docRef.createdAt.toDate());
} })
这会产生一串语法错误,我找不到解决方法。
下一次尝试
然后我尝试制作一个新表单,看看是否可以在没有用户表单的身份验证方面进行探索。
我有一个表单输入为:
this.props.firebase.db.collection("insights").add({
title: title,
text: text,
// createdAt1: new Date(),
createdAt: this.props.firebase.fieldValue.serverTimestamp()
})
在之前的表单中,new Date() 尝试在数据库中记录日期,在此示例中,createdAt 和 createdAt1 的两个字段都生成相同的数据库条目:
<div>{item.createdAt.toDate()}</div>
<div>{item.createdAt.toDate()}</div>
当我尝试输出日期的值时,第一个会产生一个错误,上面写着:
对象作为 React 子级无效(发现时间:2019 年 12 月 15 日星期日 21:33:32 GMT+1100(澳大利亚东部夏令时间))。如果你打算 渲染一组孩子,使用数组代替
第二个产生错误说:
TypeError: 无法读取未定义的属性“toDate”
我不知道下一步该尝试什么。
我看到this post 表明以下内容可能会做一些有用的事情:
{item.createdAt1.Date.valueOf()}
它没有。它呈现一个错误,上面写着:
TypeError: 无法读取未定义的属性“日期”
这个post 似乎遇到了和我一样的麻烦,但没有详细说明他们如何设法显示他们存储的日期值。
这个post 似乎卡在数组错误消息中,但似乎已经弄清楚如何使用 createdAt.toDate() 显示日期
【问题讨论】:
标签: javascript reactjs firebase google-cloud-firestore unix-timestamp