Reputation: 1964
I've made a react component which logs each time my cat has been fed. However at the moment it doesn't automatically create a new empty log for each day. I'm trying to add a method to create a new entry at midnight each day but in order for it to work with the existing component i've made, each new entry needs to be named in the 'DD-MM-YYYY' format. Whilst I can rework the component to not use that, it's handy at a glance when I go into firebase to see the dates a log has been created and would prefer not to change it.
So here is my problem: Whenever I try to do the following, firebase ignores my date format and puts a random key in instead:
this.midnightCheck = () => {
// If it is midnight, update the state
if(moment().format('HH:mm') === '00:00') {
this.dbroot.push(moment().format('DD-MM-YYYY')).child('date').set(moment().format('DD-MM-YYYY'))
}
}
I've also tried this.dbroot.set() but that doesn't work as it requires the childref to exist first.
Update: I have just tried the following which technically worked but it also overwrote the entire ref instead of just adding a new date to the ref.
this.midnightCheck = () => {
// If it is midnight, update the state
if(moment().format('HH:mm') === '18:34') {
console.info('midnightCheck is updating the state')
this.dbroot.child(moment().format('DD-MM-YYYY')).set({
date : moment().format('DD-MM-YYYY'),
logs : {}
})
}
}
Any suggestions?
Upvotes: 0
Views: 118
Reputation: 599776
When you call push()
, Firebase writes the data to a new location with an auto-generated push ID.
If you want to determine the name of a new child yourself, call child()
:
var date = moment().format('DD-MM-YYYY');
this.dbroot.child(date).child('date').set(date)
Upvotes: 1