【发布时间】:2020-05-21 23:32:55
【问题描述】:
我有问题。我想从登录的用户那里获取所有信息。这些信息包含在 Firebase 数据库中。当我使用用户数据登录时,控制台会给我以下错误消息:
subscribe.js:159 TypeError: db.collection is not a function
at Object.next (index.js:9)
我该如何解决这个错误?
index.js
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
// User is signed in.
var db = firebase.database();
var user = firebase.auth().currentUser;
var docRef = db.collection("users").doc(user.uid);
docRef.get().then(function(doc) {
if (doc.exists) {
console.log("Document data:", doc.data());
} else {
// doc.data() will be undefined in this case
console.log("No such document!");
}
}).catch(function(error) {
console.log("Error getting document:", error);
});
if(user != null){
var email_id = user.email;
}
} else {
}
});
index.html
<script>
// Your web app's Firebase configuration
var firebaseConfig = {
...
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
</script>
<script src="index.js"></script>
【问题讨论】:
-
你肯定先打电话给
firebase.initializeApp? db 对象是什么样子的 - 你可以试试 console.log() 吗? -
感谢您的评论。请看我的编辑!使用
console.log(db)我得到:e {Ie: e, B: t, INTERNAL: e} B: t {repo: e, path: e, Ae: e, De: false} INTERNAL: e {database: e} Ie: e {H: e, app: e, dataUpdateCount: 0, G: null, K: e, …} app: (...) __proto__: Object -
您要使用哪个数据库?实时数据库还是 Firestore?
-
@DougStevenson 我使用 Firestore
-
您需要使用
firebase.firestore()来获取正确的对象,而不是firebase.database()。
标签: javascript html firebase