【发布时间】:2022-07-27 22:08:00
【问题描述】:
我正在尝试使组件数组根据数据库中的 ID 显示用户的用户名,如果用户不存在,则显示“已删除”。我的函数工作正常,并且值显示在日志中,但实际渲染的组件在值应该是的位置有一个空字符串。
postsList = this.state.postsArray.map(post => {
var author = "";
AccountService.getUsernameFromId(post.author, function(id, username) {
author = username;
console.log(author);
});
return(
<Item key={post._id}>
<Item.Image size="small" src={post.image} />
<Item.Content>
<Item.Header as="a" href={"/blog/post/?id=" + post._id}>{post.name}</Item.Header>
<Item.Meta>{author} | {post.date_created}</Item.Meta>
<Item.Description>{post.post_contents.slice(0, 200) + "..."}</Item.Description>
</Item.Content>
</Item>
);
});
有人可以帮我解决这个问题吗?
【问题讨论】:
-
AccountService.getUsernameFromId 是异步函数吗?您是否正在向地图中的 API 服务器发出请求?
标签: reactjs