addDeposit(account, deposit) {
let depositsDoc = this.db.collection("accounts")
.doc(account.id)
.collection("deposits")
.doc("deposits");
return new Promise((resolve, reject) => {
deposit.created_at = firebase.firestore.FieldValue.serverTimestamp();
depositsDoc.update({
"deposits": firebase.firestore.FieldValue.arrayUnion(deposit)
})
.then((a) => {
resolve('success');
})
.catch((error) => {
reject("failed");
});
})
.then((res) => {
return res;
})
.catch((error) => {
return error;
})
}
**Firestore with angular (Using default firebase SDK not angularfire ) **
I was trying to add timestamp to the deposit object by directly adding a "created_at" property calling the timestamp. By doing this, I am getting the error titled above. How can I add timestamp to a object in an array of objects in firestore?
Use this
let deposit.created_at= firebase.firestore.Timestamp.now();
More information can be found at https://firebase.google.com/docs/reference/js/firebase.firestore.Timestamp#now
You can't use FieldValue.serverTimestamp()
as the value to union (add) or remove, to or from, an array type value of a document field. If you want to use that timestamp value, you need to pass it directly to a field you're updating or setting. This is true for everything in the FieldValue
class. That class is named this way for a reason - it applies only to field values, not elements of arrays. Note that nested map fields do count as field values. Any involvement of an array in the path is not viable.
You'll have to think of another way you structure your data that meets your needs and also satisfies Firestore requirements.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With