admin管理员组文章数量:1405140
I have a collection of settings that I am trying to save for users that login. I'm not finding a built in way to allow me to handle updating timestamps for records that I add.
db = firebase.firestore()
settingsCollection = db.collection('settings')
let userSetting = fb.settingsCollection.doc(this.userId)
//store the settings in firebase
var setWithMerge = userSetting.set({
createdOn: new Date(),
updatedOn: new Date(),
filters: {showTestOrders: show},
userId: this.userId
}, {merge: true}).then(ref => {
//console.log(ref)
}).catch(err => {
console.log(err)
})
According to docs, the .set()
method will create or update records. ( )
Can someone suggest an efficient way to handle the timestamps? My current approach always updates the createdOn
method since records exist. I'd like to NOT update that record if it already exists. I was hoping there is a convenient way to do this.
Thanks!
I have a collection of settings that I am trying to save for users that login. I'm not finding a built in way to allow me to handle updating timestamps for records that I add.
db = firebase.firestore()
settingsCollection = db.collection('settings')
let userSetting = fb.settingsCollection.doc(this.userId)
//store the settings in firebase
var setWithMerge = userSetting.set({
createdOn: new Date(),
updatedOn: new Date(),
filters: {showTestOrders: show},
userId: this.userId
}, {merge: true}).then(ref => {
//console.log(ref)
}).catch(err => {
console.log(err)
})
According to docs, the .set()
method will create or update records. ( https://firebase.google./docs/firestore/manage-data/add-data )
Can someone suggest an efficient way to handle the timestamps? My current approach always updates the createdOn
method since records exist. I'd like to NOT update that record if it already exists. I was hoping there is a convenient way to do this.
Thanks!
Share Improve this question edited Mar 26, 2023 at 8:42 Renaud Tarnec 83.2k10 gold badges98 silver badges129 bronze badges Recognized by Google Cloud Collective asked Oct 18, 2018 at 20:11 thinderythindery 1,1944 gold badges24 silver badges41 bronze badges2 Answers
Reset to default 5Another possibility is to use a set of Cloud Functions for Firebase that are triggered with onCreate()
and onUpdate()
. In other words the creation and update dates are written in the backend.
See the following answer: How to add timestamp to every collection insert,update in firebase functions for firestore database
You could perform a transaction on that document, and query and check it in the transaction handler. If the document already exists, you can also see what fields it contains, and only update that document as you see fit from your checks.
But it might be easier if your client code has a sense of when a document is definitely created the first time.
本文标签: javascriptGoogle Firestore with updated and created timestampsStack Overflow
版权声明:本文标题:javascript - Google Firestore with updated and created timestamps - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744877218a2629990.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论