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

Angular Firebase分别为每种产品设置新数据,angular,typescript,firebase,google-cloud-firestore,favorites,Angular,Typescript,Firebase,Google Cloud Firestore,Favorites,我有这个firebase数据库,我正试图将我点击的每个产品保存在数据库中,以供当前用户在那一刻访问。问题是,我只能设置1个文档,但我希望在收藏夹文档中分别设置每个新文档 我想将我的'uniqID'参数(即我单击到收藏夹的产品的id)传递到数据库,并自动创建具有其id的新文档。。我尝试了es6结构,但没有成功 firebase.service.ts async addFavorites( uniqID: string, favorite_title: string, fa

我有这个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!");
    }
  }
异步添加收藏夹(
uniqID:string,
最喜欢的标题:字符串,
最喜欢的描述:字符串,
最喜欢的图片:字符串,
最喜欢的标签:字符串,
最喜欢的stoc:布尔,
最喜欢的:数字,
最喜欢的价格:数字,
favorite\u userId:string
):承诺{
var user=firebase.auth().currentUser;
如果(用户!=null){
返回等待这个。火炉店
.收集(“用户”)
.doc(user.uid)
.设置(
{
收藏夹ID:{
//这里我需要使用参数“uniqID”将这个数组乘以我单击的每个乘积
唯一的:{
收藏夹标题:收藏夹标题,
收藏夹描述:收藏夹描述,
收藏夹图像:收藏夹图像,
收藏夹标签:收藏夹标签,
最爱的人:最爱的人,
最爱的人:最爱的人,
最优惠价格:最优惠价格,
favorite\u userId:favorite\u userId,
是的,
},
},
},
{merge:true}
)
.catch((错误)=>{
控制台日志(err);
});
}否则{
log(“请登录以添加收藏夹!”);
}
}
您需要按如下方式使用:

  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);
    });
您需要按如下方式使用:

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