从响应而不是对象获取数组 [英] Get array from response instead of Object
问题描述
我试图仅从数据库中获取特定项目,然后将其显示在表中.
I am trying to get only specific items from my database then display it in table.
这是我的sql查询的样子
This is how my sql query looks like
public async aliasesListByDomain(req: Request, res: Response): Promise<void> {
const { domain } = req.params;
const aliases = await pool.query('SELECT * FROM virtual_aliases INNER JOIN virtual_domains ON virtual_aliases.domain_id = virtual_domains.id WHERE virtual_aliases.domain_id = ?', [domain]);
if (aliases.length > 0) {
res.json(aliases);
} else {
res.status(404).json({ message: "Alias doesn't exists" });
}
}
有我的Aliases.service
There is my Aliases.service
getAliasesByDomain(domain: string): Observable<Alias> {
return this.http.get(`${this.API_URI}/aliases/aliaseslistbydomain/${domain}`);
}
这是我的组成部分
getAliasesByDomain() {
const token = this.getToken();
let user;
let domain;
console.log(token);
if(token){
user = token.split('.')[1];
user = window.atob(user);
user = JSON.parse(user);
domain = user.domain;
}
this.aliasesService.getAliasesByDomain(domain).subscribe(
res => {
this.alias = res;
},
err => console.error(err)
);
}
和组件html
<tr *ngFor="let aliases of alias;">
我的问题是我得到了错误:
My problem is that I got error:
AliasesListComponent.html:17错误错误:找不到其他支持对象的别名:输入字符串".NgFor仅支持绑定到Iterables,例如数组.
AliasesListComponent.html:17 ERROR Error: Cannot find a differ supporting object 'to sa aliases: [object Object],[object Object]' of type 'string'. NgFor only supports binding to Iterables such as Arrays.
因为我的响应是Object对象而不是array.我该如何解析?
Because my response is Object object instead of array. How can I parse this?
推荐答案
如果您的回调函数为您提供了一个数组,您可以像这样在服务内部指定它
If your callback gives you an array you can just specify it inside the service like this
getAliasesByDomain(domain: string): Observable<Alias[]> {
return this.http.get<Alias[]>(`${this.API_URI}/aliases/aliaseslistbydomain/${domain}`);
}
这篇关于从响应而不是对象获取数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!