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

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

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

问题

[HttpGet("GetUsersListByRoleAsync")]
public async Task<ActionResult<IEnumerable<UserSimple>>> GetUsersListByRoleAsync(int roleId)
{
    try
    {
        IEnumerable<UserSimple> result = await aRepo.GetUsersListByRoleAsync(roleId);
        return Ok(result);
    }
    catch (Exception ex)
    {
        return BadRequest(ex.Message);
    }
}
public class UserSimple
{
    public int Id_User { get; set; }
    public string DomainName { get; set; }
}
async GetUsersListByRoleAsync(roleId: number): Promise<UserSimple[]> {
    const resp = await fetch(`api/Administration/GetUsersListByRoleAsync?roleId=${roleId}`, {credentials: "include"})

    if (resp.status >= 400)
        throw new Error(`[AdministrationDbHandler]: ${await resp.text()}`)

    const usersList = await resp.json() as UserSimple[]
    return usersList
}
export type UserSimple = {
    id_User: number
    domainName: string
}

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

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

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


<details>
<summary>英文:</summary>

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:
```cs
[HttpGet(&quot;GetUsersListByRoleAsync&quot;)]
public async Task&lt;ActionResult&lt;IEnumerable&lt;UserSimple&gt;&gt;&gt; GetUsersListByRoleAsync(int roleId)
{
    try
    {
        IEnumerable&lt;UserSimple&gt; result = await aRepo.GetUsersListByRoleAsync(roleId);
        return Ok(result);
    }
    catch (Exception ex)
    {
        return BadRequest(ex.Message);
    }
}

UserSimple class:

public class UserSimple
{
    int Id_User { get; set; }
    string DomainName { get; set; }
}

Function in the frontend DbHandler:

async GetUsersListByRoleAsync(roleId: number): Promise&lt;UserSimple[]&gt; {
    const resp = await fetch(`api/Administration/GetUsersListByRoleAsync?roleId=${roleId}`, {credentials: &quot;include&quot;})

    if (resp.status &gt;= 400)
        throw new Error(`[AdministrationDbHandler]: ${await resp.text()}`)

    const usersList = await resp.json() as UserSimple[]
    return usersList
}

UserSimple type in the frontend:

export type UserSimple = {
    id_User: number
    domainName: string
}

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

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

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

答案1

得分: 1

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

public class UserSimple
{
    public int Id_User { get; set; }
    public string DomainName { get; set; }
}
英文:

The properties in class UserSimple should be public:

public class UserSimple
{
    public int Id_User { get; set; }
    public string DomainName { get; set; }
}

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:

确定