最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Google Firestore with updated and created timestamps - Stack Overflow

programmeradmin1浏览0评论

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 badges
Add a ment  | 

2 Answers 2

Reset to default 5

Another 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.

发布评论

评论列表(0)

  1. 暂无评论