javascript - 与 Cloud Firestore 使用react

标签 javascript reactjs firebase google-cloud-firestore

我正在尝试在我的 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 文档:

https://firebase.google.com/docs/firestore/quickstart

关于javascript - 与 Cloud Firestore 使用react,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59143005/

相关文章:

javascript - CSS/JS slider 代码阻塞 CSS/JS 覆盖

javascript - 在 Node.js 中构建多分支树时的性能问题

python - Flask,为 React 应用程序提供服务 : cannot refresh "pages"

reactjs - react .js |如何消除 Codesandbox 中的跨域错误

node.js - 在 Express 中通过客户端路由为多个 React 应用程序提供服务

android - 查询具有大数据集的 Firebase 数据库非常非常慢

javascript - 关于如何使用其中的元素对 ul 的每一侧进行编号的任何想法?

Javascript 数据传输和输入类型=文件问题

firebase - 如何从 Firebase 函数调用 Python 函数

java - 更新后不要连接到 firebase