React – 从后端接收的数组元素没有参数

huangapple go评论95阅读模式
英文:

React – The elements of array received from backend has no paramteres

问题

  1. [HttpGet("GetUsersListByRoleAsync")]
  2. public async Task<ActionResult<IEnumerable<UserSimple>>> GetUsersListByRoleAsync(int roleId)
  3. {
  4. try
  5. {
  6. IEnumerable<UserSimple> result = await aRepo.GetUsersListByRoleAsync(roleId);
  7. return Ok(result);
  8. }
  9. catch (Exception ex)
  10. {
  11. return BadRequest(ex.Message);
  12. }
  13. }
  1. public class UserSimple
  2. {
  3. public int Id_User { get; set; }
  4. public string DomainName { get; set; }
  5. }
  1. async GetUsersListByRoleAsync(roleId: number): Promise<UserSimple[]> {
  2. const resp = await fetch(`api/Administration/GetUsersListByRoleAsync?roleId=${roleId}`, {credentials: "include"})
  3. if (resp.status >= 400)
  4. throw new Error(`[AdministrationDbHandler]: ${await resp.text()}`)
  5. const usersList = await resp.json() as UserSimple[]
  6. return usersList
  7. }
  1. export type UserSimple = {
  2. id_User: number
  3. domainName: string
  4. }

问题是,后端调试时一切正常,但在前端,resp.json() as UserSimple[] 返回以下内容:

  1. 0: {}
  2. length: 1
  3. [[Prototype]]: Array(0)

因此,它具有所需数量的元素,但它们是空的。可能的问题是什么?

  1. <details>
  2. <summary>英文:</summary>
  3. I am using React + C# for my website. I&#39;m trying to get some data from database, but seem to have some troubles. Here&#39;s the function in my controller:
  4. ```cs
  5. [HttpGet(&quot;GetUsersListByRoleAsync&quot;)]
  6. public async Task&lt;ActionResult&lt;IEnumerable&lt;UserSimple&gt;&gt;&gt; GetUsersListByRoleAsync(int roleId)
  7. {
  8. try
  9. {
  10. IEnumerable&lt;UserSimple&gt; result = await aRepo.GetUsersListByRoleAsync(roleId);
  11. return Ok(result);
  12. }
  13. catch (Exception ex)
  14. {
  15. return BadRequest(ex.Message);
  16. }
  17. }

UserSimple class:

  1. public class UserSimple
  2. {
  3. int Id_User { get; set; }
  4. string DomainName { get; set; }
  5. }

Function in the frontend DbHandler:

  1. async GetUsersListByRoleAsync(roleId: number): Promise&lt;UserSimple[]&gt; {
  2. const resp = await fetch(`api/Administration/GetUsersListByRoleAsync?roleId=${roleId}`, {credentials: &quot;include&quot;})
  3. if (resp.status &gt;= 400)
  4. throw new Error(`[AdministrationDbHandler]: ${await resp.text()}`)
  5. const usersList = await resp.json() as UserSimple[]
  6. return usersList
  7. }

UserSimple type in the frontend:

  1. export type UserSimple = {
  2. id_User: number
  3. domainName: string
  4. }

The thing is, while debugging in the backend everything is great, but in the frontend, resp.json() as UserSimple[] returns the following:

  1. 0: {}
  2. length: 1
  3. [[Prototype]]: Array(0)

So, it has the needed amount of elements, but they're empty. What can be the problem?

答案1

得分: 1

UserSimple 中的属性应该是公共的:

  1. public class UserSimple
  2. {
  3. public int Id_User { get; set; }
  4. public string DomainName { get; set; }
  5. }
英文:

The properties in class UserSimple should be public:

  1. public class UserSimple
  2. {
  3. public int Id_User { get; set; }
  4. public string DomainName { get; set; }
  5. }

huangapple
  • 本文由 发表于 2023年6月6日 16:06:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76412585.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定