0

I was following along this tutorial to connect Firebase using react hooks. I followed everything but I am having a problem interacting with the real-time database on my project. For example, when I tried to insert data into the database, it doesn't make any changes it just shows null; I tried to see if it was other problems, tried it with different accounts, creating new projects, but nothing seems to work.

My App.js looks like this:

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

I checked the rules for the realtime database on firebase, and it shows:

{
  "rules": {
    ".read": "now < 1610427600000",  // 2021-1-12
    ".write": "now < 1610427600000",  // 2021-1-12
  }
}
My app runs properly, but on the console, since I was logging the XHRHttpRequests, it shows something is failing on the GET request.

enter image description here

It is a newly created project and I only changed the App.js and added firebase.js that looks exactly like the one in the guide.

2
  • 2
    You have no code that checks the result of add() for errors. It could be failing and you'd never know. Also, the security rules you have here are for Realtime Database but your code is writing to Firestore. They are completely different database systems. Commented Dec 14, 2020 at 4:10
  • Thank you I changed the add() to Realtime Database version and it worked Commented Dec 14, 2020 at 4:59

1 Answer 1

3

The security rules you show are for the Firebase Realtime Database, but the code you have is accessing Cloud Firestore. While both databases are part of Firebase, they are completely separate and have different APIs.

To write to the Realtime Database would look something like this:

firebase
    .database()
    .ref("notes")
    .push({
        title: "Working",
        body: "This is to check the Integration is working",
    });
Sign up to request clarification or add additional context in comments.

1 Comment

I tried to upvote but not enough reputation so I clicked on the checkmark (✓) to accept it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.