How can I add Multiple subject in an array using firebase(firestore) React Native code?

Viewed 28
1 Answers
import firebase from "firebase";
const firebaseConfig = {
      apiKey: process.env.REACT_APP_FIREBASE_KEY,
      authDomain: process.env.REACT_APP_FIREBASE_DOMAIN,
      databaseURL: process.env.REACT_APP_FIREBASE_DATABASE,
      projectId: process.env.REACT_APP_FIREBASE_PROJECT_ID,
      storageBucket: process.env.REACT_APP_FIREBASE_STORAGE_BUCKET,
      messagingSenderId: process.env.REACT_APP_FIREBASE_SENDER_ID,
      appId: process.env.REACT_APP_FIREBASE_APP_ID,
      measurementId: process.env.REACT_APP_FIREBASE_MEASUREMENT_ID,
    };
    const app = firebase.initializeApp(firebaseConfig);
    const db = app.firestore();
    
    const studentID="XXXXXX";
    const conllectionName="YYYYY";
    
    //to array
    await db.collection(conllectionName).doc(studentID).update({
      Subjects: firebase.firestore.FieldValue.arrayUnion("Music"),
    });
    
    //update a attribute of data(doc)
    await db.collection(conllectionName).doc(studentID).update({
      StudentName: "NewName",
    });
    
    //add new data(doc)
    await db.collection(conllectionName).doc(studentID).set({
     StudentName:"name",
     Subject:[...],
    });

Maybe you can read the above code to find more infomation about that you need...

About the variables, i think little case at first is better...

Ex: studentName vs StudentName

At last, let's keep studying and find the way out!

Related