1

I need to store a server timestamp to my firestore document
from the internet I got this code firebase.firestore.FieldValue.serverTimestamp() but after I using this code in firestore it stored as like this _methodName FieldValuea.serverTimestamp
What is the issue and how to fix this? my ts code `

 let m1 = new Message();
    m1.doctor = this.uid;
    m1.patient = this.messageList[this.messageList.length - 1].patient;
    m1.isPatient = false;
    m1.message = event.message;


    m1.date = firebase.firestore.FieldValue.serverTimestamp(),
      m1.reply = true,
      // type: files.length ? 'file' : 'text',
      //files: files,
      m1.user = {
        name: 'Jonh Doe',
        avatar: 'https://i.gifer.com/no.gif',
      },

      this.firestore.collection(Config.chatCollection).add(JSON.parse(JSON.stringify(m1)));
`



class Message {
  user = {
    name: 'Jonh Doe',
    avatar: 'https://i.gifer.com/no.gif',
  }
  reply;
  doctor = "";
  text;
  date;
  cdate;
  isPatient = true;
  message = "";
  patient = "";
  time = "";
  documentID = "";
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Midhilaj
  • 4,905
  • 9
  • 45
  • 88

1 Answers1

0

This is most probably because you are doing

this.firestore.collection(Config.chatCollection).add(JSON.parse(JSON.stringify(m1)));

By doing

this.firestore.collection(Config.chatCollection).add(m1);

it should be fine, since it seems that m1 is a JavaScript Object.

The doc for the add() method is here, you will see that its parameter shall be an "Object containing the data for the new document". If m1 is a valid Object, you do not need to do JSON.parse(JSON.stringify(m1)), which corresponds to:

  1. Transforming the m1 JavaScript object into a JSON string, and then;
  2. Taking the resulting JSON string and transforming it back into a JavaScript object.
Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
  • ERROR FirebaseError: Function CollectionReference.add() requires its first argument to be of type object, but it was: an object – Midhilaj Mar 31 '20 at 10:21
  • Updated in question – Midhilaj Mar 31 '20 at 10:23
  • Can you check if `m1` is an Object. E.g. by using https://stackoverflow.com/questions/8511281/check-if-a-value-is-an-object-in-javascript – Renaud Tarnec Mar 31 '20 at 10:25
  • PS: are you sure that your code does not contain the typo we can see in your question: `_methodName FieldValuea.serverTimestamp`. `FieldValuea` with an extra `a` instead of `FieldValue` – Renaud Tarnec Mar 31 '20 at 10:28