[英]Angular - Get this ERROR Error: Error trying to diff '[object Object]'. Only arrays and iterables are allowed
Decided to find out what all the fuss was about with Angular.决定找出 Angular 的所有问题。 Got a demo app working with a JSON file (using db.json and JSON server from the angular CLI).
获得了一个使用 JSON 文件的演示应用程序(使用 db.json 和 Z0ECD11C1D7A287401D148A287401D148A23BBD7A2F8Z 服务器,来自 ZD18B8624A0F5F622DDDACLI823)。 All working great.
一切都很好。
So I get adventurous and decide to build a C# API (as that's the long plan anyway).所以我开始冒险并决定构建一个 C# API (因为这是一个长期的计划)。
Straight away I ran into the CORS problem, and to solve in my ASP.NET config I have (startup.cs)我马上遇到了 CORS 问题,并在我的 ASP.NET 配置中解决(startup.cs)
app.UseCors( options =>
{
options
.WithOrigins("http://localhost:4200/")
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
I output my JSON like this (done this like a thousand times in all my apps)我 output 我的 JSON 像这样(在我所有的应用程序中这样做了一千次)
return Json(new { tasks = _context.Tasks });
In my angular app, I have在我的 angular 应用程序中,我有
//private apiUrl = 'http://localhost:5101/tasks'; //<- this is using json server
private apiUrl = "https://localhost:44363/home/tasks"; //<- this is my asp.net api
constructor(private http: HttpClient) {}
getTasks(): Observable<Task[]> {
return this.http.get<Task[]>(this.apiUrl);
}
My web service spits out the exact same JSON (char for char) as the JSON server but in my browser console (after dealing with the cors problem ) I get this: My web service spits out the exact same JSON (char for char) as the JSON server but in my browser console (after dealing with the cors problem ) I get this:
ERROR Error: Error trying to diff '[object Object]'.
ERROR 错误:尝试比较“[object Object]”时出错。 Only arrays and iterables are allowed
仅允许 arrays 和可迭代对象
I've read lots of articles on here about this error but non seem to deal with this situation, (none that I could find anyway) and I checked a fair few我在这里阅读了很多关于这个错误的文章,但似乎没有处理这种情况,(无论如何我都找不到),我检查了一些
What am I missing?我错过了什么?
This is the JSON这是 JSON
{
"tasks": [
{
"id": 1,
"text": "Doctors Appointment",
"day": "1st January 2022 at 10:30 AM",
"reminder": false
},
{
"id": 2,
"text": "Meeting",
"day": "5th July 2022 at 11:45 AM",
"reminder": false
},
{
"id": 3,
"text": "Car Servicing",
"day": "15th October 2022 at 8:30 AM",
"reminder": false
},
{
"id": 4,
"text": "Cleaner",
"day": "3rd November 2022 at 10:00 AM",
"reminder": false
},
{
"id": 5,
"text": "Dinner",
"day": "6th July 2022 at 7:50 PM",
"reminder": false
}
]
}
Based on your JSON, HttpGet expects to receive the data model as below:根据您的 JSON,HttpGet 预计会收到如下数据 model:
export interface TaskResponse {
tasks: Task[];
}
And with .pipe(map)
to return response.tasks
as Observable<Task[]>
.并使用
.pipe(map)
返回response.tasks
作为Observable<Task[]>
。
import { map } from 'rxjs/operators';
getTasks(): Observable<Task[]> {
return this.http
.get<TaskResponse>(this.apiUrl)
.pipe(map((response: TaskResponse) => response.tasks));
}
This error is just because you are trying to loop an object.这个错误只是因为你试图循环一个 object。 Just take the data into a result like
只需将数据转换为类似的结果
this.getTasks().subscribe(result => {
this.data = result.tasks;
})
After taking the response in data you can use the loop.在数据中获取响应后,您可以使用循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.