如何在Angular 7中使用复杂表存储到Firebase

时间:2019-08-28 04:57:42

标签: angular firebase google-cloud-firestore

我在FIrebase中有一个具有以下结构的数据库:

Collection     Document       Collection               Document
Shooters    => SOME DRINK =>  Name: "SOME DRINK"
                              Price: 50.50
                              Ingredients          =>  "Ingredient One"    =>  Price: 3.11
                                                                               Volume: 2
                                                       "Ingredient Two"    =>  Price: 5.50
                                                                               Volume: 3
                                                       "Ingredient Three"  =>  Price: 1.50
                                                                               Volume: 1

我有一个像这样的模型:

export class Drink {
    Name: string;
    OrderType: string;
    Price: number;
    Ingredient: Ingredient[];
}

export class Ingredient {
    Name: string;
    Detail: IngredientDetail
}

export class IngredientDetail {
    Price: number;
    Volume: number;
}

这是我在控制台中获得的数据: enter image description here

我需要将此值保存在我的Firestore数据库中。我想出了这段代码...

public AddDrink = (model: Drink) => {
    const data = Object.assign({}, model);
    console.log(data);
    this.fireStore.collection(model.Name).add(model.Name)
  }

能否请您告诉我如何将这种模型存储到我的Firestore中?我看到的大多数教程都是将一个数据保存到表中。非常感谢。

请参考下图: enter image description here

enter image description here

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

如果您尝试将文档添加到子集合中,请尝试以下示例:

public AddDrink = (model: Drink) => {
    const data = Object.assign({}, model);
    console.log(data);
    this.fireStore.collection('Shooters').doc('B52').collection('Ingredients').add(...)
  }

在此repo中,您可以看到有关此类问题的示例。只需看看searchesCollection const