Firebase中难以设置数据的结构定义

问题描述

enter image description here

更新: 因此,基本上这是我的firebase结构,我编辑了用户集合,以得到一个名为ListaFavorite的特殊列表,该列表列出了用户将在会话中添加的所有收藏项。问题是,我必须在原始UserInterface文件中定义与firebase中完全相同的结构。

我声明的favoriteObj存储了item字段中的值,并将其带到firebase,但是由于我无法在接口中使用对象,我该如何将结构设置为favoriteObj呢?

对不起,如果我没有清楚自己的意思,但这基本上就是我想要的。

我正在尝试匹配我的结构,即:

 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,};
从Firebase

到我的用户界面:

export interface User {
  uid: string;
  email: string;
  displayName: string;
  photoURL: string;
  emailVerified: boolean;
  FavoriteID: string;
  SavedFavorite: boolean;
  AddcosID: string;
  Savedcos: boolean;
  ListaFavorite: ... // here i must pass the favoriteObj
}

之后,我必须在函数包括更新数据:

SetUserData(user) {
    const userRef: AngularFirestoreDocument<any> = this.afStore.doc(
      `users/${user.uid}`
    );

    const userData: User = {
      uid: user.uid,email: user.email,displayName: user.displayName,photoURL: user.photoURL,emailVerified: user.emailVerified,FavoriteID: null,SavedFavorite: null,AddcosID: null,Savedcos: null,ListaFavorite : ...
    };
    return userRef.set(userData,{
      merge: true,});
  }

我的意思是将收藏的对象标记到当前用户界面中,然后根据登录到收藏页面用户调用所有对象。 我真的不知道如何在这里传递obj。有帮助吗?

解决方法

要将对象添加到接口,您需要执行所谓的嵌套接口。您应该为favoriteObj创建一个接口:

interface FavoriteObj {
      favorite_title: string,favorite_description: string,favorite_image: string,...
}

然后使用[key: string]将其嵌套在用户界面中,这就是我们所说的indexable types

export interface User {
  uid: string;
  email: string;
  displayName: string;
  photoURL: string;
  emailVerified: boolean;
  FavoriteID: string;
  SavedFavorite: boolean;
  AddCosID: string;
  SavedCos: boolean;
  ListaFavorite: {
       [key: string]: FavoriteObj
  }
}