【问题标题】:Firebase not adding data to Realtime database in ReactFirebase 未将数据添加到 React 中的实时数据库
【发布时间】:2021-03-24 18:20:44
【问题描述】:

我跟随 this tutorial 使用反应挂钩连接 Firebase。我关注了所有内容,但在与项目中的实时数据库交互时遇到问题。例如,当我尝试向数据库中插入数据时,它没有进行任何更改,它只是显示为 null;我尝试查看是否是其他问题,尝试使用不同的帐户,创建新项目,但似乎没有任何效果。

我的App.js 看起来像这样:

import React from "react"
import "./App.css"
import firebase from "./firebase"
function App() {
    firebase
        .firestore()
        .collection("notes")
        .add({
            title: "Working",
            body: "This is to check the Integration is working",
        });
    return (
        <div className="App">
            <h2>Note Taking App</h2>
            <p>using React Hooks and Firebase</p>
            <h3>Notes : </h3>
        </div>
    )
}
export default App

我检查了firebase上实时数据库的规则,它显示:

{
  "rules": {
    ".read": "now < 1610427600000",  // 2021-1-12
    ".write": "now < 1610427600000",  // 2021-1-12
  }
}
我的应用程序运行正常,但在控制台上,因为我记录了XHRHttpRequests,,它显示GET 请求失败了。

这是一个新创建的项目,我只更改了App.js 并添加了firebase.js,看起来与指南中的完全一样。

【问题讨论】:

  • 您没有代码可以检查add() 的结果是否有错误。它可能会失败,你永远不会知道。此外,您在此处拥有的安全规则适用于实时数据库,但您的代码正在写入 Firestore。它们是完全不同的数据库系统。
  • 谢谢,我将add() 更改为实时数据库版本,它工作正常

标签: node.js reactjs firebase firebase-realtime-database


【解决方案1】:

您显示的安全规则适用于 Firebase 实时数据库,但您拥有的代码是访问 Cloud Firestore。虽然这两个数据库都是 Firebase 的一部分,但它们是完全独立的并且具有不同的 API。

写入实时数据库看起来像这样:

firebase
    .database()
    .ref("notes")
    .push({
        title: "Working",
        body: "This is to check the Integration is working",
    });

【讨论】:

  • 我尝试投票但没有足够的声誉所以我点击复选标记 (✓) 接受它
猜你喜欢
  • 2021-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-20
  • 2021-08-28
  • 2019-10-19
相关资源
最近更新 更多