Vueでfirestoreからデータを取得し、dataのLogに格納したい
解決したいこと
Vueでfirestoreからデータを取得し、dataのLogに格納したいのですが、うまくいかないです。様々なサイトで調べてやってみているのですがどれもエラーが出てしまいます。3つ試しました。
発生している問題・エラー1
db.collection is not function
該当するソースコード1
// ユーザーがログインしているか確認
firebase.auth().onAuthStateChanged(user => {
if (user) {
// ユーザーがログインしている場合、Firestoreからデータを取得
const userUid = user.uid; // ユーザーのUIDを取得
db.collection('card')
.doc()
.where('userId', '==', userUid) // ユーザーのUIDと一致するデータをクエリでフィルタリング
.get()
.then(querySnapshot => {
querySnapshot.forEach(doc => {
// ドキュメントのデータを取得
const cardData = doc.data();
console.log(cardData);
});
})
.catch(error => {
console.error('データの取得エラー:', error);
});
} else {
alert("ログインしてください。")
this.$router.push("/login")
}
});
発生している問題・エラー2
db.collection is not function
該当するソースコード2
const auth = getAuth()
const user = auth.currentUser
const db = getFirestore()
if (user) {
const docRef = getFirestore(
collection(db, "card").doc().where("uid", "==", user.uid)
)
const docSnap = await getDoc(docRef)
console.log("Document data:", docSnap.data())}
else {
alert("ログインしてください。")
this.$router.push("/login")
}
const docRef = の後を、
query(collection(db, "card").doc().where("uid", "==", user.uid))
にしても
collection(db, "card").doc().where("uid", "==", user.uid)
としてもnot functionとなる
import query from "forebase/firestore"等のインポートはしています。
発生している問題・エラー3
TypeError: Cannot use 'in' operator to search for '_delegate' in undefined
該当するソースコード3
const auth = getAuth()
const user = auth.currentUser
const db = getFirestore()
if (user) {
this.Log = getDoc(db.fishcard, where("uid", "==", user.uid))
console.log(this.Log)
} else {
alert("ログインしてください。")
this.$router.push("/login")
}
データ取得の方法を知っている方がいらっしましたら教えていただきたいです。お願いいたします。
0 likes