Renan A.
Renan A.

Reputation: 21

Firebase - How to insert the current date in the Timestamp field inside an array?

"FieldValue.serverTimestamp() cannot be used inside of an array"

Is there any way to add the current date in a Timestamp field within an array? The structure of the collection is:

nameCollection (collection):
   nameDocument (document | array | map) [
      - date (Timestamp);
      - origin (string);
      - quantity(number);
   ];
   (...)

const admin = require('firebase-admin');
...
array.nameDocument.push({
        date: admin.firestore.FieldValue.serverTimestamp(),
        origin: "xxx",
        quantity: 10
    });

Upvotes: 1

Views: 403

Answers (2)

Try this:

const admin = require('firebase-admin');
...
array.nameDocument.push({
    date: admin.firestore.Timestamp.fromDate(new Date()),
    origin: "xxx",
    quantity: 10
});

Upvotes: 2

Simon
Simon

Reputation: 1737

For java it's like this Timestamp.now(). Should be very similar in JS. Check the following link.

Upvotes: 0

Related Questions