How to write a document firestore

Viewed 37

I'm trying to write a new document like this:

database.collection('NegociosDev').doc('Peluquerías').collection('Negocios').doc('PR01').collection('citas').add({
            CheckIn: formattedDate(citaDate) + ' ' + formattedTime(horaSelec),
            CheckOut: fechaFinal.toString(),
            Dirección: 'Avenida Los Majuelos 54',
            Negocio: 'PRIVILEGE SALONES',
            Precio: pre.toString(),
            Servicio: servicioSeleccionado.toString(),
            extraInformation: empleadoSelect.toString(),
            idUsuario: idUser.toString(),
        })

But I'm getting this error everytime I try to write anything (I can read it):

Unhandled Promise Rejection: FirebaseError: Missing or insufficient permissions.

Using this rules:

rules_version = '2';
 service cloud.firestore {
  match /databases/{database}/documents {
   match /{document=**} {
    allow write: if request.auth.uid != null;
    allow read;
  }
 }
}

I'm not doing any authentication, I'm just trying to write some stuff, but I don't want to change the rules.

2 Answers

I'm not doing any authentication, I'm just trying to write some stuff, but I don't want to change the rules.

You won't be able to write if you don't authenticate (based on that rule)/

If you decide to remove the rile allow write: if true, be aware this is not secure, you can set it back after you are done with your tests.

Otherwise, you can authenticate your user, documents are useful for this purpose

Maybe check the local emulator, maybe it might help you, docs are here

Related