简体   繁体   中英

Firebase firestore not saving array data

I have been trying to solve this for the past few days. I am trying to save data to a firebase collection. However, firebase refuses to save an array of strings. Everything is saved except for the array, which shows as empty. But I am certain the array is not empty. I even print out right in the function, and it works.

uploadTask(taskForm: any, attachedFiles: Array<any>) {
    console.log(attachedFiles);
    const task = this.fireStore
      .collection('tasks')
      .doc(this.userId + uuidv4())
      .set({
        title: taskForm.title,
        description: taskForm.description,
        subject: taskForm.subject,
        urgency: taskForm.urgency,
        budget: taskForm.budget,
        time: taskForm.time,
        files: attachedFiles,
      })
      .then(() => {
        console.log('Document successfully written!');
      })
      .catch((error) => {
        console.error('Error writing document: ', error);
    });
}

When You use 'set' you are modifying a document that already exists using an id or field of that document. If I understand your question, You are trying to create a completely new document and so you must use the 'add' method instead. Remember firestore will automatically create its own unique id for each document created. You don't need to specify the id.

your code should therefore read as

 uploadTask(taskForm: any, attachedFiles: Array<any>) { const task = this.fireStore.collection('tasks').add({ /*<===use add here*/ title: taskForm.title, description: taskForm.description, subject: taskForm.subject, urgency: taskForm.urgency, budget: taskForm.budget, time: taskForm.time, files: attachedFiles, }).then(() => { console.log('Document successfully written;'). }).catch((error) => { console:error('Error writing document, '; error); }); }

This answer might help you [difference between firestore set and add][1]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM