我在 API 中有一条路线,可以让用户使用roles. 在这条路线中,如果我输入用户的姓名,那么我将获得所有roles分配给他/她的信息。但问题是它只返回第一个结果。如果该角色存在于另一个对象中,则不会显示该角色。所以我对该行发表评论return,一切正常,但随着结果,我收到错误消息:"user not found"你们能告诉我我在做什么错误吗?
谢谢你。
路线 -
GET("/users/:username", controllers.GetUserByRole)
控制器 -
func GetUserByRole(c *gin.Context) {
paramId := c.Param("username")
.............
.............
.............
var newUsers []models.User
iter := client.Collection("users").Documents(ctx)
for {
doc, err := iter.Next()
if err == iterator.Done {
break
}
if err != nil {
log.Fatalf("Failed to iterate: %v", err)
}
var tempUsers models.User
if err := doc.DataTo(&tempUsers); err != nil {
break
}
newUsers = append(newUsers, tempUsers)
}
for _, a := range newUsers {
for _, element := range a.Roles{
if element == paramId {
c.IndentedJSON(http.StatusOK, a)
return //if I comment this line line then I geting message `"user not found"` along with results
}
}
}
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "user not found"})
}
URL:http://localhost:3000/users/Analyst
响应应该是:
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"roles": ["Developer", "Analyst"],
},
{
"id": 3,
"name": "Clementine Bauch",
"username": "Samantha",
"roles": ["Manger", "Analyst"],
}
但我是这样的(只有第一个匹配的):
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"roles": ["Developer", "Analyst"],
}
如果我评论该return行,则响应为(期望的结果 + 错误消息):
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"roles": ["Developer", "Analyst"],
},
{
"id": 3,
"name": "Clementine Bauch",
"username": "Samantha",
"roles": ["Manger", "Analyst"],
}{
"message": "album not found"
}
素胚勾勒不出你
BIG阳
相关分类