英文:
Firestore timestame using toDate() is not a function error
问题
我有一个带有时间戳的 Firestore 数据。
用户将数据备份为 JSON 文件 - 导出功能
const dataStr = JSON.stringify(todos);
let dataUri = 'data:application/json;charset=utf-8,' + encodeURIComponent(dataStr);
let fileName = 'data.json';
let linkElement = document.createElement('a') as HTMLAnchorElement;
linkElement.setAttribute('href', dataUri);
linkElement.setAttribute('download', fileName);
linkElement.click();
然后用户将恢复数据 - 导入功能
const uploadFile = fileInput.files[0];
const fileReader = new FileReader();
fileReader.onload = async (e) => {
const dataStr = e.target?.result as string;
const newDatas = JSON.parse(dataStr) as todosProps[];
console.log(newDatas);
settodos([
...todos,
...newDatas
]);
try {
newDatas.map(async (d) => {
await setDoc(doc(collectionRef, d.id.toString()), {
...d,
});
});
console.log('完成导入');
} catch (error) {
console.error(error);
}
}
我注意到,Firestore 时间戳对象在转换为 JSON 时具有不同的构造函数方法。
所以我无法在导入的对象中使用像 ".toDate() .toMillis" 这样的 Firestore 函数。那么为什么它与原始时间戳不同呢?也许是因为我将原始数据转换为 JSON,或者我的代码有问题?
我不知道该尝试什么。我原本期望能够获得正确的顺序。
英文:
I have firestore data with timestamp.
User will backup data as JSON file -
Export-Function
const dataStr = JSON.stringify(todos);
let dataUri = 'data:application/json;charset=utf-8,' + encodeURIComponent(dataStr);
let fileName = 'data.json';
let linkElement = document.createElement('a') as HTMLAnchorElement;
linkElement.setAttribute('href', dataUri);
linkElement.setAttribute('download', fileName);
linkElement.click();
Then User will restore data- Import-Function
const uploadFile = fileInput.files[0];
const fileReader = new FileReader();
fileReader.onload = async (e) => {
const dataStr = e.target?.result as string;
const newDatas = JSON.parse(dataStr) as todosProps[];
console.log(newDatas);
settodos([
...todos,
...newDatas
]);
try {
newDatas.map(async (d) => {
await setDoc(doc(collectionRef, d.id.toString()), {
...d,
});
});
console.log('finish import');
} catch (error) {
console.error(error);
}
I noticed that , firestore timestamp object value have different constructor method when convert to JSON.
so I cannot use firestore function like " .toDate() .toMillis " in imported one . So why it is different with original Timestamp. May be I converted original data to JSON . Or my code is wrong ?
I have no idea to try . I was expecting to get correct order .
答案1
得分: 1
Credits @Rahul Kumar
对于那些出现 "toDate() 不是函数" 错误的人,可能是因为您在 JSON 对象上使用它。您可以简单地使用从 Firebase 接收的时间戳对象,或者如果无法直接使用它,那么要将其转换回时间戳对象,您可以这样做:
const timestamp = new firebase.firestore.Timestamp(jsonTimestamp.seconds, jsonTimestamp.nanoseconds)
现在执行 timestamp.toDate()
将有效。
英文:
Credits @Rahul Kumar
for anyone who is having toDate() is not a function error might be because you are using it on a JSON object. You can simply use the timestamp object received from firebase, or If you are not able to use it directly then To convert it back to timestamp object you can do this:
const timestamp = new firebase.firestore.Timestamp(jsonTimestamp.seconds, jsonTimestamp.nanoseconds)
now doing timestamp.toDate() will work
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论