2

I'm trying to get back some info about the document I just created/updated without making a second request.

I know that I get back a Document reference with the Id of the document, but I need additional info like createTime and updateTime.

Currently, this is the way I'm trying to achieve that, but I would like to spare that extra request since it's not very efficient.

const docRef = await database.collection('tagsCollection').add({
    ...input
    createTime: firestore.FieldValue.serverTimestamp()
});

const doc = await docRef.get();

return {
    ...doc.data(),
    id: doc.id,
    createTime: doc.data().createTime.toMillis().toString()
};
Samuel E.
  • 2,320
  • 2
  • 26
  • 31
  • So where do createTime and updateTime come from, if not your client code? Are you using a server timestamp? Something in Cloud Functions? There must be something else going on here that I can't see. – Doug Stevenson Jan 08 '19 at 22:13
  • It is added automatically by Firestore, but you can assume that I added it because that is basically what I’m trying to do. – Samuel E. Jan 08 '19 at 22:15
  • I didn't notice it wasn't in the docs. But no, it's not a private implementation, those are properties returned from the `docRef.get()` query. – Samuel E. Jan 08 '19 at 22:20

1 Answers1

0

Since those fields come from Firestore on the server side, it's safe to assume that the client doesn't know what they are until a DocumentSnapshot has been queried. You could perhaps make a guess on your own, but you're essentially trying to guess what the round trip latency is for your call to add().

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • I did add some properties of my own for tracking creation and update times before finding out that firestore adds those automatically. In that case I used `serverTimestamp`, however, I had the same issue because the `serverTimestamp` is set on the server, but I wanted to return it. The issue is not specific to those fields, its just in general. ill update the code above for better example. – Samuel E. Jan 08 '19 at 22:25
  • Unless the actual value originates on the client, I don't think you're going to be able to use it without making a query. It sounds like you might be trying to optimize something that's not even a performance problem yet? – Doug Stevenson Jan 08 '19 at 22:49