为什么 doc.data() 不是此 if 语句中的函数?
Why isn't doc.data() a function in this if-statement?
当我删除 if 语句时,我得到了数据并且 doc.data() 有效,但我需要检查类型 'added' 所以我不会一次又一次地输出现有数据。
使用 if 语句时,我收到此错误消息:
Uncaught TypeError: doc.data is not a function
async getChats(callback){
const roomFilter = query(colRef, where("room", "==", this.room));
const ordered = query(roomFilter, orderBy('created_at'));
this.unsub = onSnapshot(ordered, (snapshot) => {
let items = []
snapshot.docChanges().forEach(doc => {
if(doc.type === 'added'){
items.push({ ...doc.data(), id: doc.id })
console.log(items);
callback(items);
}})
});
snapshot.docChanges().forEach(doc => {
您称为 doc
的变量不仅仅是文档。它包含有关更改的其他信息。使用您选择的变量名称,您需要执行以下操作:
items.push({ ...doc.doc.data(), id: doc.doc.id })
不过我会考虑重命名它,也许是“更改”
snapshot.docChanges().forEach(change => {
if (change.type === 'added') {
items.push({ ...change.doc.data(), id: change.doc.id })
console.log(items);
callback(items);
}
})
当我删除 if 语句时,我得到了数据并且 doc.data() 有效,但我需要检查类型 'added' 所以我不会一次又一次地输出现有数据。
使用 if 语句时,我收到此错误消息:
Uncaught TypeError: doc.data is not a function
async getChats(callback){
const roomFilter = query(colRef, where("room", "==", this.room));
const ordered = query(roomFilter, orderBy('created_at'));
this.unsub = onSnapshot(ordered, (snapshot) => {
let items = []
snapshot.docChanges().forEach(doc => {
if(doc.type === 'added'){
items.push({ ...doc.data(), id: doc.id })
console.log(items);
callback(items);
}})
});
snapshot.docChanges().forEach(doc => {
您称为 doc
的变量不仅仅是文档。它包含有关更改的其他信息。使用您选择的变量名称,您需要执行以下操作:
items.push({ ...doc.doc.data(), id: doc.doc.id })
不过我会考虑重命名它,也许是“更改”
snapshot.docChanges().forEach(change => {
if (change.type === 'added') {
items.push({ ...change.doc.data(), id: change.doc.id })
console.log(items);
callback(items);
}
})