Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add document to firestore database using react native

We are trying add documents values manually using react native app to firestore DB.

Here is the reference code we found for web

db.collection("cities").doc("LA").set({
    name: "Los Angeles",
    state: "CA",
    country: "USA"
})
.then(function() {
    console.log("Document successfully written!");
})
.catch(function(error) {
    console.error("Error writing document: ", error);
});

For react native we used this code

firebase.firestore().collection('users').doc("hello").add({
        title: this.state.textInput,
        complete: false,
      })

I'm getting error like add us not defined

How to add documents and collections to it ?

like image 950
Afsara Avatar asked Dec 07 '22 16:12

Afsara


2 Answers

You can add a document to a collection. You cannot add a document to a document.

To add a document to your users collection:

firebase.firestore().collection('users').add({
  title: this.state.textInput,
  complete: false,
})

So remove the .doc("hello") from there.

like image 78
Frank van Puffelen Avatar answered Dec 11 '22 08:12

Frank van Puffelen


Here I found the solution for my data structure

firebase
  .firestore()
  .collection("Users")
  .doc("mydoc")
  .collection("Activities")
  .doc("Database")
  .set({
    key: "1",
    value: "",
  })
  .then((ref) => { console.log(ref) });
like image 31
Afsara Avatar answered Dec 11 '22 07:12

Afsara