I'm trying to figure out how to add data to cloud firestore from my react app.
I have it all working for the data entered in a form, but am missing something because when I try to add a createdAt timestamp, I get an error.
My current attempt is:
import React from "react";
import { Link } from 'react-router-dom'
import { useStateMachine } from "little-state-machine";
import updateAction from "./updateAction";
import { fsDB, firebase, settings } from "../../../firebase";
const Result = props => {
const { state } = {
useStateMachine(updateAction),
createdAt: firebase.firestore.FieldValue.serverTimestamp()
};
fsDB
.collection("project")
.add(state)
return (
<div>
<pre>{JSON.stringify(state, null, 2)}</pre>
</div>
);
};
export default Result;
If i remove the createdAt and just upload state, it all works fine.
I'm getting stuck on how to add the timestamp.
Can anyone see where I'm going wrong?
I have found this post which sets out a snap shot to merge a date field with a record. I'm struggling to understand if there is something about snapshot which needs the record to be created before a date can be added? It doesn't seem logical that I have to create a record before I can merge a date field into it.