json - 当我将一组对象放入 Firefox(使用打字稿)并获取数据时,我无法将其作为对象数组获取吗?
问题描述
从 Firebase 数据库保存和检索数据不会给我相同类型的数据。下面我将解释我在做什么以及我遇到了什么:
保存数据
我正在Recipe
使用以下代码将数组保存到 Firebase 数据库:
save() {
this.http.put(
this.recipeURL,
this.recipesService.getRecipes()
).subscribe(
(response) => {
console.log(response);
}
);
}
使用 检索对象Array
的代码非常简单:Recipe
RecipeService
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
我想要替换的对象的原始列表。似乎我正在检索对象,Array
而Json
不是Recipe
对象。当您查看控制台输出时,您可以在下面找到两者之间差异的屏幕截图。
如您所见,一个是 的Array
,{...}
另一个是Array
的Recipe
。
希望有人可以帮助解释/解决这个问题。问题是,在我分配从 Firebase 数据库获得的数据并更新我的应用程序(使用 a Subject
)之后,我的页面没有显示新数据,而是继续显示旧数据。
解决方案
事实证明,这个问题完全不同。RecipeService
提供了错误的组件。
推荐阅读
- spring-mvc - 如何将 Thymeleaf th:field 与 java 8 LocalDate 绑定
- postgresql - 如何使用 PostgreSQL 和 TypeORM 进行查询)其中表具有 JSON 类型列
- c++ - `IDispatch::GetTypeInfo()` 接口是否正常?
- amazon-web-services - 每条路由有 1 个 Lambda 函数会更好吗?还是 1 个处理子路由的 Lambda?
- c# - 在分配给模型之前验证 {} 值
- python - pygal 在 x 轴上绘制日期
- pandas - 'record_path' 处的字典在 json_normalize 中没有变平
- php - 从 index.php 访问另一个文件
- jmeter - Jmeter - 如何在 POST 数据中排列值
- web-applications - 现代网络应用架构