Firestore的`toDate()`不是一个函数错误

huangapple go评论70阅读模式
英文:

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,或者我的代码有问题?

Firestore的`toDate()`不是一个函数错误

我不知道该尝试什么。我原本期望能够获得正确的顺序。

英文:

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 ?

Firestore的`toDate()`不是一个函数错误

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

huangapple
  • 本文由 发表于 2023年3月31日 22:22:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75899641.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定