英文:
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'm trying to get some data from database, but seem to have some troubles. Here's the function in my controller:
```cs
[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);
}
}
UserSimple
class:
public class UserSimple
{
int Id_User { get; set; }
string DomainName { get; set; }
}
Function in the frontend DbHandler
:
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
}
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; }
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论