带有更新和创建时间戳的 Google Firestore

thi*_*ery 1 javascript firebase vue.js google-cloud-firestore

我有一组设置,我试图为登录的用户保存这些设置。我没有找到一种内置的方式来允许我处理我添加的记录的更新时间戳。

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)
 })
Run Code Online (Sandbox Code Playgroud)

根据文档,该.set()方法将创建或更新记录。(https://firebase.google.com/docs/firestore/manage-data/add-data

有人可以建议一种处理时间戳的有效方法吗?createdOn由于记录存在,我当前的方法总是更新方法。如果该记录已经存在,我不想更新该记录。我希望有一种方便的方法来做到这一点。

谢谢!

Ren*_*nec 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