Firebase 分别为每个产品设置新数据

Firebase set new data for each product separately

我有这个 firebase 数据库,我正在尝试将我点击的每个产品保存在数据库中,供当时访问的当前用户收藏。问题是,我只能设置1个文档,但我想在收藏文档中分别拥有每个新文档。

我想将我的 'uniqID' 参数(即我点击收藏的产品的 ID)传递给数据库,并使用它的 ID 自动创建新文档。我尝试使用 es6 结构,但没有没用。

firebase.service.ts

async addFavorites(
    uniqID: string,
    favorite_title: string,
    favorite_description: string,
    favorite_image: string,
    favorite_tag: string,
    favorite_stoc: boolean,
    favorite_reducere: number,
    favorite_price: number,
    favorite_userId: string
  ): Promise<void> {
    var user = firebase.auth().currentUser;

    if (user != null) {
      return await this.firestore
        .collection("users")
        .doc(user.uid)
        .set(
          {
            favoriteID: {
              // here i need to multiply this array by each product i click using parameter "uniqID"
              uniqueID: {
                favorite_title: favorite_title,
                favorite_description: favorite_description,
                favorite_image: favorite_image,
                favorite_tag: favorite_tag,
                favorite_stoc: favorite_stoc,
                favorite_reducere: favorite_reducere,
                favorite_price: favorite_price,
                favorite_userId: favorite_userId,
                isFavorite: true,
              },
            },
          },
          { merge: true }
        )
        .catch((err) => {
          console.log(err);
        });
    } else {
      console.log("Please Log In to add favorite!");
    }
  }

您需要按如下方式使用square brackets notation

  const favoriteObj = {};
  favoriteObj[uniqId] = {
    favorite_title: favorite_title,
    favorite_description: favorite_description,
    favorite_image: favorite_image,
    favorite_tag: favorite_tag,
    favorite_stoc: favorite_stoc,
    favorite_reducere: favorite_reducere,
    favorite_price: favorite_price,
    favorite_userId: favorite_userId,
    isFavorite: true,
  };

  return await this.firestore
    .collection('users')
    .doc(user.uid)
    .set({ favoriteID: favoriteObj }, { merge: true })
    .catch((err) => {
      console.log(err);
    });