kamil danilski
kamil danilski

Reputation: 68

How to set date to firebase?

I try to first time use firebase. I created extremely open rules:

service cloud.firestore {
  match /databases/{database}/documents {
    // Make sure the uid of the requesting user matches name of the user
    // document. The wildcard expression {userId} makes the userId variable
    // available in rules.
    match /users/{userId} {
      allow read, update, delete, create: if 1 == 1;
 match /{document=**} {
        allow read, write, update, delete, create: if 2 == 2;
      }
}
  }
}

then I wanted to send my test message to the serwer (I successfully instal login/register and reset app, and now, my user are logged).

import { useAuthState } from "react-firebase-hooks/auth";
import { auth, db, logout } from "./firebase";
import { query, collection, getDocs, where } from "firebase/firestore";
import { getDatabase, ref, set } from "firebase/database";

     const SaveDateBase = () => {
          e.preventDefault();
  db.collection('Books').set({
            title: "pokemon"
          })
         }
return (<>
(...)
<button type="submit" onSubmit={SaveDateBase}>Save changes</button>
</>

Needles to say, that I created "Books" collection with one example of my work.

Upvotes: 0

Views: 116

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

Problem in your rules

Your rules allow reading and writing a top-level users collection, and all data under there. But your code tries to write to a top-level Books collection. Since none of your rules mention this collection, that write is rejected.


Problem in your code

In addition this code won't work:

db.collection('Books').set({
  title: "pokemon"
})

You're trying to write directly to the Books collection, which is not an option (and should be giving you a pretty explicit error). If you want to add a document to the Books collection:

db.collection('Books').add({
  title: "pokemon"
})

If you want to set a document, you'll need to specify the document ID too:

db.collection('Books').doc('MyFirstPokemonBook').add({
  title: "pokemon"
})

Upvotes: 2

Related Questions