TypeError: snap.data is not a function - Firestore Database
TypeError: snap.data is not a function - Firestore Database
我在 React 中有以下辅助函数:
import { getAuth } from 'firebase/auth';
import { initializeApp } from 'firebase/app';
import { getFirestore, collection, doc, addDoc, getDoc, setDoc, getDocs, query, where } from 'firebase/firestore';
import { getStorage, ref } from 'firebase/storage';
import { FIREBASE_API } from '../config';
const firebaseApp = initializeApp(FIREBASE_API);
const auth = getAuth(firebaseApp);
const db = getFirestore(firebaseApp);
const storage = getStorage();
export function getDocuments(col) {
const colRef = collection(db, col);
const q = query(colRef, where('uid', '==', auth.currentUser.uid));
getDocs(q).then((snap) => {
console.log(snap);
return snap?.data();
});
return [];
}
我正在尝试 return 给定集合的文档。这是我的控制台输出:
我想弄清楚为什么 data()
方法对我来说不可用。它是一个 Firestore 数据库(不是实时的)。
我是不是做错了什么?
getDocs()
returns一个QuerySnapshot (contains data of all the documents that matched your query) and not a DocumentSnapshot. It has a docs
property that is an array of QueryDocumentSnapshot可以遍历并读取接收到的所有文档的数据:
export function getDocuments(col) {
const colRef = collection(db, col);
const q = query(colRef, where('uid', '==', auth.currentUser.uid));
return getDocs(q).then((snap) => {
const data = snap.docs.map((d) => ({ id: d.id, ...d.data() }))
console.log(data);
return data;
});
}
我在 React 中有以下辅助函数:
import { getAuth } from 'firebase/auth';
import { initializeApp } from 'firebase/app';
import { getFirestore, collection, doc, addDoc, getDoc, setDoc, getDocs, query, where } from 'firebase/firestore';
import { getStorage, ref } from 'firebase/storage';
import { FIREBASE_API } from '../config';
const firebaseApp = initializeApp(FIREBASE_API);
const auth = getAuth(firebaseApp);
const db = getFirestore(firebaseApp);
const storage = getStorage();
export function getDocuments(col) {
const colRef = collection(db, col);
const q = query(colRef, where('uid', '==', auth.currentUser.uid));
getDocs(q).then((snap) => {
console.log(snap);
return snap?.data();
});
return [];
}
我正在尝试 return 给定集合的文档。这是我的控制台输出:
我想弄清楚为什么 data()
方法对我来说不可用。它是一个 Firestore 数据库(不是实时的)。
我是不是做错了什么?
getDocs()
returns一个QuerySnapshot (contains data of all the documents that matched your query) and not a DocumentSnapshot. It has a docs
property that is an array of QueryDocumentSnapshot可以遍历并读取接收到的所有文档的数据:
export function getDocuments(col) {
const colRef = collection(db, col);
const q = query(colRef, where('uid', '==', auth.currentUser.uid));
return getDocs(q).then((snap) => {
const data = snap.docs.map((d) => ({ id: d.id, ...d.data() }))
console.log(data);
return data;
});
}