我正在尝试在我的 React 应用程序中从实时数据库切换到云 Firestore。
在我的 firebase.js 中,我对用户进行了定义,如下所示。
class Firebase {
constructor() {
app.initializeApp(config).firestore();
this.auth = app.auth();
// this.db = app.firebase.database()
this.db = app.firestore();
}
doCreateUserWithEmailAndPassword = (email, password) =>
this.auth.createUserWithEmailAndPassword(email, password);
doSignInWithEmailAndPassword = (email, password) =>
this.auth.signInWithEmailAndPassword(email, password);
doSignOut = () =>
this.auth.signOut();
doPasswordReset = email =>
this.auth.sendPasswordResetEmail(email);
doPasswordUpdate = password =>
this.auth.currentUser.updatePassword(password);
// *** User API ***
user = uid => this.db.ref(`users/${uid}`);
users = () => this.db.ref('users');
}
当我使用实时数据库时,这可以获取用户,但现在我收到一条错误消息:
this.db.ref is not a function
为什么我不能再引用 this.db 以及如何导航 cloud firestore 文档以找到等效项?
我见过this post建议保留某些事情的实时数据库。是否有一个 Cloud Firestore 无法执行的操作列表,并且正在获取其中之一的用户列表?
我见过this documentation并尝试过:
user = uid => this.db.DocumentReference(`users/${uid}`);
users = () => this.db.DocumentReference('users');
这会产生与使用 this.db.ref 相同的错误消息
最佳答案
firestore中没有ref()
,如果要访问文档需要使用document()
方法:
db.collection("users").add({
first: "Ada",
last: "Lovelace",
born: 1815
})
.then(function(docRef) {
console.log("Document written with ID: ", docRef.id);
})
.catch(function(error) {
console.error("Error adding document: ", error);
});
请检查以下 Firestore 文档:
关于javascript - 与 Cloud Firestore 使用react,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59143005/