简体   繁体   English

Angular - 获取此 ERROR 错误:尝试区分“[object Object]”时出错。 仅允许 arrays 和可迭代对象

[英]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));
}

Sample Demo on StackBlitz StackBlitz 上的示例演示

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.

相关问题 尝试区分“[object Object]”时出错。 仅允许 arrays 和可迭代对象 IONIC 4 - Error trying to diff '[object Object]'. Only arrays and iterables are allowed IONIC 4 如何以角度显示json列表对象? “尝试区分 &#39;[object Object]&#39; 时出错。只允许使用数组和可迭代对象” - How to display json list object in angular? "Error trying to diff '[object Object]'. Only arrays and iterables are allowed" CategoryComponent.html:53 错误错误:尝试比较“[object Object]”时出错。 只允许数组和可迭代对象 - CategoryComponent.html:53 ERROR Error: Error trying to diff '[object Object]'. Only arrays and iterables are allowed 愚蠢的“错误错误:尝试区分“[对象对象]”时出错。 仅允许 arrays 和可迭代对象' - Stupid 'ERROR Error: Error trying to diff '[object Object]'. Only arrays and iterables are allowed' 角度5:尝试差异化&#39;on&#39;时出错。 仅允许数组和可迭代 - Angular 5 :Error trying to diff 'on'. Only arrays and iterables are allowed Angular2错误尝试diff&#39;[object Object]&#39; - Angular2 Error trying to diff '[object Object]' 角度错误:找不到“object”类型的不同支持对象“[object Object]”。 NgFor 仅支持绑定到 Iterables,例如 Arrays - Angular error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays 错误 错误:找不到支持“对象”类型的 object“[对象对象]”的不同。 NgFor 仅支持绑定到 Iterables Angular 9 - ERROR Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables Angular 9 尝试区分&#39;[object Object]&#39;`时出错,需要传递错误消息 - Error trying to diff ‘[object Object]'` needs to pass the error message 获取对象内的对象数组-尝试与&#39;[object Object]&#39;进行比较时出错 - getting an array of objects within an object - Error trying to diff '[object Object]'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM