【发布时间】:2019-05-24 11:43:16
【问题描述】:
抱歉,我不知道这是一个问题,还是我无法以正确的方式使用它,
我面临的问题是..
我正在尝试创建一个新文档,其中包含某些字段,其中一个字段被创建,当我尝试使用 onSnapshot() 侦听器检索新输入的文档时出现问题。 onSnapshot() 侦听器被触发两次,类型为 added ,然后触发类型为 modified ,只是因为 firebase.firestore.FieldValue 的值。 serverTimestamp() 没有同时插入。
这里是添加文档的代码sn-p
senderid : this.buddy.senderId,
receiverid: this.buddy.receiverId,
message : msg,
created: firebase.firestore.FieldValue.serverTimestamp()
这是阅读文档的代码:
this.db.collection(this.collectionName.friendsCollection).doc(this.buddy.docid)
.collection(this.collectionName.chatsCollection).orderBy('created')
.onSnapshot(snapshot=> {
skip.buddymessages = [];
if(snapshot.empty)
{
console.log("First Chat");
}
else{
console.log("size",snapshot.size)
snapshot.docChanges.forEach(change => {
if (change.type === 'added') {
console.log('New : ', change.doc.data());
}
if (change.type === 'modified') {
console.log('Modified : ', change.doc.data());
}
if (change.type === 'removed') {
console.log('Removed : ', change.doc.data());
}
});
这是控制台截图:-
【问题讨论】:
-
我确实有一个解决方法,只是对这个问题感到好奇。解决方法是使用 new Date() js 方法来获取当前时间戳。
标签: javascript firebase ionic-framework google-cloud-firestore