繁体   English   中英

Firebase firestore 不保存数组数据

[英]Firebase firestore not saving array data

在过去的几天里,我一直在努力解决这个问题。 我正在尝试将数据保存到 firebase 集合中。 但是,firebase 拒绝保存字符串数组。 除了显示为空的数组外,所有内容都已保存。 但我确定数组不是空的。 我什至在 function 中打印出来,它可以工作。

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);
    });
}

当您使用“设置”时,您正在使用该文档的 id 或字段修改已经存在的文档。 如果我理解您的问题,您正在尝试创建一个全新的文档,因此您必须改用“添加”方法。 请记住,firestore 将为创建的每个文档自动创建自己的唯一 ID。 您无需指定 ID。

因此,您的代码应读作

 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); }); }

这个答案可能会帮助您[firestore set 和 add 之间的区别][1]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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