Google cloud firestore 向Cloud Firestore中的嵌套对象添加时间戳

Google cloud firestore 向Cloud Firestore中的嵌套对象添加时间戳,google-cloud-firestore,google-cloud-functions,Google Cloud Firestore,Google Cloud Functions,我正在尝试将嵌套元素上的时间戳字段设置为当前时间: docRef.update({ arrayOfStuff: { id: 123, dateAdded: admin.firestore.FieldValue.serverTimestamp() } }) 我得到一个错误: FieldValue.serverTimestamp() cannot be used inside of an array TypeError: date.getTime is

我正在尝试将嵌套元素上的时间戳字段设置为当前时间:

docRef.update({
   arrayOfStuff: {
        id: 123,
        dateAdded: admin.firestore.FieldValue.serverTimestamp()
   }
})
我得到一个错误:

FieldValue.serverTimestamp() cannot be used inside of an array
TypeError: date.getTime is not a function
    at Function.fromDate (/srv/node_modules/firebase-admin/node_modules/@google-cloud/firestore/build/src/timestamp.js:108:42)
    at firestoreDB.doc.get.then.site (/srv/index.js:32:45)
使用
Date.now()

尝试
Firestore.Timestamp.fromDate()
会产生错误:

FieldValue.serverTimestamp() cannot be used inside of an array
TypeError: date.getTime is not a function
    at Function.fromDate (/srv/node_modules/firebase-admin/node_modules/@google-cloud/firestore/build/src/timestamp.js:108:42)
    at firestoreDB.doc.get.then.site (/srv/index.js:32:45)
其中index.js的第32行是:

let now = admin.firestore.Timestamp.fromDate(Date.now());
想法?

如前所述,“
FieldValue.serverTimestamp
如果不对Firestore的工作方式进行重大改进,就无法在阵列内部提供支持”。所以这种行为是意料之中的

再找一点,我就找到了雷诺·塔内克的一本书。尽管他提到您可能需要更改数据模型


我希望这会有所帮助。

因此我决定使用:
let now=admin.firestore.Timestamp.now()
在firestore中生成一个“Timestamp”值。我怀疑不同之处在于,我得到的时间戳来自代码运行时,而不是serverTimestamp(),我认为这是数据实际存储在Firestore中的时间(两次之间可能会有延迟)。请参阅使用云函数和文档中的字段(而不是数组中)的可能解决方法包含时间戳的。