【发布时间】:2018-06-17 02:18:34
【问题描述】:
当我运行一些这样的角度代码时发生了一些问题:
return this.http.get('apiurl').map(res => res as Article[]);
当我检查我的 cloudwatch 时,我发现我的日志如下所示:
{ Items:
[ { text: [Object],
description: [Object],
id: [Object],
userid: [Object],
title: [Object] },
{ text: [Object],
description: [Object],
id: [Object],
userid: [Object],
title: [Object] },
{ text: [Object],
description: [Object],
id: [Object],
userid: [Object],
title: [Object] } ],
Count: 3,
ScannedCount: 3 }
我想这意味着我可以从 DynamoDB 中获取数据,但是我的页面上显示了错误消息:
我可以在 codepen 上获取我的数据而不会出现任何错误。 我检查了很多,仍然无法弄清楚出了什么问题。谁能帮我解决这个问题?任何回复将有义务
服务代码
getArticleById(id:string){
this.dataLoaded.next(null);
this.dataLoadFailed.next(false);
return this.http.get('https://example.com/'+id)
.map(res => res as Article[]);
}
在其他页面调用服务:
articles:Article[] = [];
getAllArticles(){
this.articleService.getArticleById('all')
.subscribe((articles:Article[])=>{
this.articles = articles;
})
}
这是我通过codepen得到的数据:
[
{
"title" : "fakfjdkfjdskaf",
"description" : "fdkalfjdskalfj",
"text" : "<p>dkalfjdsklafjdksalfjsda</p><p><br></p>",
"userid" : "100",
"id" : "13",
}
, {
"title" : "title",
"description" : "desc",
"text" : "aaaaa",
"userid" : "userid",
"id" : "100",
}
, {
"title" : "tiel",
"description" : "desc",
"text" : "text",
"userid" : "di",
"id" : "12",
}
]
【问题讨论】:
-
你能把实际的 json 字符串和其他代码放在你得到错误的地方吗?
-
看起来您的 http 调用返回的 JSON 可能有问题。也可以放 JSON 吗?
-
试试 (res => res.json() as Article[]);
-
@GaurangDave 我添加了我的代码和我的数据,请检查我们的
-
JSON objects can't have a trailing comma: 所以这是无效的 JSON。使用 JSON 感知编辑器(不是 JavaScript 编辑器!)或 JSON 验证器(即查看任何一个免费的在线 JSON 验证站点)——也就是说,有现有的工具/资源可以帮助查明数据中的错误,使用它们!
标签: arrays json angular subscription angular5