我正在使用Firebase身份验证注册用户,然后通过React / Redux应用将用户详细信息保存在Firestore的/ users集合中。我添加了一个 createdAt 字段,该字段保存了Firestore服务器时间戳以标识何时创建用户。 .set调用看起来像这样:
try {
let newUser = {
displayName,
createdAt: firestore.FieldValue.serverTimestamp(),
showWizard: true,
role: "client",
status: "signedUp",
schoolName,
quiz,
email
};
await firestore.set(`users/${currentUser.uid}`, {
...newUser
});
} catch (err) {
dispatch(asyncActionError());
toastr.error("Something went wrong", err.message);
throw new SubmissionError({
_error: err.message
});
}
然后,我想检索此用户文档,以将分析数据(包括时间戳)发送给Segment。检索数据并触发Redux Segment操作的调用如下所示:
try {
const newUserDoc = await firestore.get(`users/${currentUser.uid}`);
const newUserRecord = {
id: newUserDoc.id,
...newUserDoc.data()
};
console.log(newUserRecord);
dispatch({
type: USER_SIGN_UP,
meta: {
analytics: [
{
eventType: EventTypes.identify,
eventPayload: {
userId: newUserRecord.id,
traits: {
email: newUserRecord.email,
createdAt: newUserRecord.createdAt.toDate().toISOString(),
displayName: newUserRecord.displayName,
quiz: newUserRecord.quiz,
schoolName: newUserRecord.schoolName,
role: newUserRecord.role
}
}
},
{
eventType: EventTypes.track,
eventPayload: {
event: "User Signed Up",
properties: {
category: "Admin",
label: newUserRecord.quiz
}
}
}
]
}
});
dispatch(asyncActionFinish());
return newUserRecord;
} catch (error) {
dispatch(asyncActionError());
toastr.error("Something went wrong", error.message);
throw new SubmissionError({
_error: error.message
});
}
用户文档显示没有任何错误,但是 createdAt 字段显示为空。稍后,当我在Firebase控制台中签入时,createdAt字段将填充正确的日期。
在我检索文档时,似乎尚未完成服务器时间戳的创建。我如何确保获取文档会包含正确的时间戳?
感谢您的帮助!