简体   繁体   English

如何将 object 添加到 Firebase 字段集合中的数组?

[英]How can I add an object to an array inside a Firebase Field Collection?

I'm trying to create a to-do style app.我正在尝试创建一个待办事项风格的应用程序。 I'm trying to create a card that contains a name and tags field.我正在尝试创建一个包含名称和标签字段的卡片。 Here's an example of my data structure.这是我的数据结构的示例。 I want to create another cards array but for id: "nhl".我想创建另一个卡片数组,但要使用 id:“nhl”。

[![enter image description here][1]][1] [![在此处输入图片描述][1]][1]

I want to create a cards array (everything inside the blue box) but for the 1 object with the id:"nhl".我想创建一个卡片数组(蓝色框内的所有内容),但要创建 ID 为“nhl”的 1 object。

Here is what my createCard function looks like:这是我的 createCard function 的样子:

      const createCard = async () => {
    await updateDoc(doc(db, "users", currentUser.uid), {
      labels: arrayUnion({
        activeLabel: [
          {
            name: name,
            tags: tags,
          },
        ],
      }),
    });
    getData();
    findLabel(activeLabel);
    // toast.success("Tags Created!");
    console.log("Tags Created");
  };

Here is where the function is being called:这是调用 function 的地方:

 return (
    <div className="cards">
      <div className="cards__create">
        <input
          onChange={(e) => setName(e.target.value)}
          className="cards__input"
          placeholder="Enter title..."
        ></input>
        <textarea
          onChange={(e) => setTags(e.target.value)}
          className="cards__input cards__input--textarea"
          placeholder="Enter tags..."
        ></textarea>
        <img
          onClick={createCard}
          className="cards__add"
          src={addIcon}
          alt="plus sign icon"
        ></img>
      </div>

arrayUnion() cannot work in your case. arrayUnion()不适用于您的情况。 As explained in the doc , "each specified element that doesn't already exist in the array will be added to the end."正如文档中所解释的那样,“数组中尚不存在的每个指定元素都将添加到末尾。”

So, instead of updating the second element of the labels array, arrayUnion() will create a new array element with the same id fiels id:"nhl" and the two cards.因此, arrayUnion()不会更新标签数组的第二个元素,而是会创建一个具有相同id字段id:"nhl"和两张卡片的新数组元素。

You need to first read the Firestore document, modify the labels Array in your front-end and write it back to Firestore.你需要先阅读Firestore文档,修改你前端的labels数组,然后写回Firestore。

暂无
暂无

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

相关问题 我可以在 angular firebase 集合的数据字段内插入、更新和删除数组吗? - Can I insert, update and delete array inside a data field in angular firebase collection? Firebase:将集合放入数组中 - Firebase: put collection inside an array 如何在 QuerySnapshot 中获取集合 - How can I get a collection inside a QuerySnapshot 无法在 firebase/firestore 的数组中删除具有特定值的 object - Can't delete an object with a certained value inside an array in firebase/firestore Firebase Firestore iOS:如何将图像保存到 Firestore 中的集合中? - Firebase Firestore iOS: How can I save images to a collection in the firestore? 无法更新 firebase 集合字段 - 预期类型为“ya”,但它是:自定义 Ia object - Can't update firebase collection field - Expected type 'ya', but it was: a custom Ia object 如何使用 firebase-admin 删除集合中的字段? - How to delete a field in a collection with firebase-admin? firestore 集合中的字段数组中可以存储多少个 id - How many ids can be stored in a field array in firestore collection 给定一个 uid 数组,如何在 React Native 中返回 Firebase 数据的 JavaScript 对象? - How can I return a JavaScript object of Firebase data in React Native, given an array of uids? 如何获取 Firebase 存储可下载链接数组并将其设置为 object 以存储在 mongodb 数据库中? - How can I get Firebase Storage downloadable array of links and set it to object for storing in mongodb database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM