首页 > 解决方案 > 当我将一组对象放入 Firefox(使用打字稿)并获取数据时,我无法将其作为对象数组获取吗?

问题描述

从 Firebase 数据库保存和检索数据不会给我相同类型的数据。下面我将解释我在做什么以及我遇到了什么:

保存数据

我正在Recipe使用以下代码将数组保存到 Firebase 数据库:

save() {
  this.http.put(
    this.recipeURL,
    this.recipesService.getRecipes()
  ).subscribe(
    (response) => {
      console.log(response);
    }
  );
}

使用 检索对象Array的代码非常简单:RecipeRecipeService

getRecipes() {
  return this.recipes.slice();
}

Recipe模型定义如下(没什么花哨的):

export class Recipe {
  public id: number;
  public name: string;
  public description: string;
  public imagePath: string;
  public ingredients: Ingredient[];

  constructor(id: number, name: string, desc: string, imagePath: string, ingredients: Ingredient[]) {
    this.id = id;
    this.name = name;
    this.description = desc;
    this.imagePath = imagePath;
    this.ingredients = ingredients;
  }
}

检索数据

之后,我使用以下代码检索数据:

read() {
  this.http.get<Recipe[]>(
    this.recipeURL
  ).subscribe((recipes) => {
    console.log(recipes);
    console.log(this.recipesService.getRecipes());
  })
}

结果/问题

在代码中,我记录了 get 方法的响应以及Recipe我想要替换的对象的原始列表。似乎我正在检索对象,ArrayJson不是Recipe对象。当您查看控制台输出时,您可以在下面找到两者之间差异的屏幕截图。

在此处输入图像描述

如您所见,一个是 的Array{...}另一个是ArrayRecipe

希望有人可以帮助解释/解决这个问题。问题是,在我分配从 Firebase 数据库获得的数据并更新我的应用程序(使用 a Subject)之后,我的页面没有显示新数据,而是继续显示旧数据。

标签: jsonangulartypescripthttpget

解决方案


事实证明,这个问题完全不同。RecipeService提供了错误的组件。


推荐阅读