angular - Angular如何从数据库中获取数组并将元素推入其中
问题描述
我正在使用 Firebase,我有一个带有单词的“字典”,每个单词都有不同的定义。我正在尝试从后端获取包含特定单词定义的数组并将新定义添加到其中。
数据应该是这样的:
words
'Word1'
'Definitions'
'Definition1'
'Definition2'
'Word2'
'Definitions'
'Definition1'
我所做的是,但我无法从值更改中获取属性“定义”:
let data = {
name: word,
definitions: [],
}
let newDefinition = 'Some definition'
let obj = this.db.object(`words/${word}`);
let obs = obj.valueChanges().subscribe(x => {
changes = x;
// If the word doesn't exist, create it.
if (x === null) {
data.definitions.push(this.newDefinition);
obj.set({ definitions: data.definitions, word: data.word });
// If the word exists add the definition to its list.
} else {
data.definitions = x.definitions;
data.definitions.push(this.newDefinition);
obj.update({ definitions: data.definitions });
}
obs.unsubscribe();
});
解决方案
最简单的方法是将x
to 转换为any
:
data.definitions = (<any>x).definitions;
但是为了完整性,您应该使用 TypeScript 接口来定义从数据库中获取的数据的结构。
推荐阅读
- twitter-bootstrap - ASP NET Core 引导下拉菜单不起作用
- php - 将“设计选项”选项卡添加到 Wordpress/WPBakery 中的自定义简码
- shopify - 如何在 Shopify 在线商店 2.0 中的一个部分中拥有超过 12 个块?
- reactjs - 有没有办法从嵌套的 [slug] 路径重定向 404 以使用 Next.js 中的父文件?
- sql-server - 授予用户“MachineName\Administrator”的权限不足以执行此操作。(rsAccessDenied)
- java - Spring Data Jpa 用于多个 findBy 查询的一种方法
- reactjs - 获取 FLatList keyExtractor 的 key 使网络调用反应原生
- mysql - 在 SQL 中平均电影评分
- android - 如何在android中标记新产品并在一段时间后删除“新”标签
- javascript - ParseError: Unexpected token = 尝试在 Google Apps 脚本编辑器中保存时