是否可以在 Firestore 中使用动态对象属性 [英] Is it possible to use dynamic object properties in Firestore

查看:26
本文介绍了是否可以在 Firestore 中使用动态对象属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法将动态变量(如时间戳)存储为 Firestore 中的属性?我尝试了以下但不起作用.(error: 'timeStamp' 被赋值但从未使用过)

Is there a way to store dynamic variables, like a timestamp, as a property in Firestore? I tried the following but that does not work. (error: 'timeStamp' is assigned a value but never used)

var timeStamp = new Date()
db.collection('test').doc('jdW0spuKzLcq7ycmDNm2').update({
        users: {
          timeStamp: {
            this: 'bla',
            that: 'blabla'
          }
        }
      })

我正在寻找的是一种存储数据的方法,如下所示:

What I am looking for is a way to store data as follows:

用户{120220190800{this:'bla',that'blabla'},120220190802{this:'blablabla',that'bla'}

users{120220190800{this:'bla',that'blabla'},120220190802{this:'blablabla',that'bla'}

我也想知道是否可以通过以下方式存储数据:

I am also wondering if it is possible to store data in the following way:

用户{{this:'bla',that'blabla'},{this:'blablabla',that'bla'}

users{{this:'bla',that'blabla'},{this:'blablabla',that'bla'}

推荐答案

我用过这样的:

db.collection('test').doc('doc').get().then ( (docSnap) => {
        var timeStamp = new Date()
        var doc = docSnap.data()        
        doc.users[timeStamp.toString()] = {'this': 'bla',that: 'blabla'} 
        db.collection('test').doc('doc').set(doc);
})

几次执行后的结果如下,所以我认为如果你重新格式化日期应该是你需要的:

The result after few execution is bellow, so I think if you reformat the date should be something you need:

享受!

这篇关于是否可以在 Firestore 中使用动态对象属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆