0

I have a JSON object coming from a Firebase (Firestore) query.

let json = [{
  "historical_data": {
    "created_by": "HKyUyCuVj0g12htOsAPmGCcffBq2",
    "created_at": {
      "seconds": 1606403054,
      "nanoseconds": 416000000
    }
  }
}];

I need to update the created_at value to a Firestore server timestamp.

Therefore I would iterate and update the respective field like this:

for (var i in json) {
  var item = json[i];
  item.historical_data.created_at = firebase.firestore.FieldValue.serverTimestamp();
}

Although VS Code is giving me the following error message:

Type 'FieldValue' is missing the following properties from type '{ seconds: number; nanoseconds: number; }': "seconds", "nanoseconds"

I understand the error and why it's happening, so is there any way to update the JSON and change or cast the type of created_at to another one?

1 Answer 1

1

You can directly cast the return type of serverTimestamp with the as keyword in typescript:

interface CreateAt {
    seconds: number;
    nanoseconds: number;
}

for (var i in json) {
  var item = json[i];
  item.historical_data.created_at = 
    firebase.firestore.FieldValue.serverTimestamp() as CreateAt;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.