I have one route in API where I can get users with roles
. In this route, if I put the name of the user then I will get all the roles
assigned to him/her. But the problem is that it returns only the first result. If that role is present in another object then it is not getting displayed. So I comment on the return
line and everything works fine but along with the result, I am getting the error message:"user not found"
Can you guys please tell me what mistake I am doing?
Thank you.
Route -
GET("/users/:username", controllers.GetUserByRole)
Controller -
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
Response Should be:
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"roles": ["Developer", "Analyst"],
},
{
"id": 3,
"name": "Clementine Bauch",
"username": "Samantha",
"roles": ["Manger", "Analyst"],
}
but I am getting like this (only the first matching one):
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"roles": ["Developer", "Analyst"],
}
If I comment the return
line then response is (desired result error message):
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"roles": ["Developer", "Analyst"],
},
{
"id": 3,
"name": "Clementine Bauch",
"username": "Samantha",
"roles": ["Manger", "Analyst"],
}{
"message": "album not found"
}
Complete API response -
[
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"roles": ["Developer", "Analyst"],
},
{
"id": 2,
"name": "Ervin Howell",
"username": "Antonette",
"roles": ["Assistant"],,
},
{
"id": 3,
"name": "Clementine Bauch",
"username": "Samantha",
"roles": ["Manger", "Analyst"],
}
]
CodePudding user response:
You can do like this
var userList []models.User
for _, a := range newUsers {
for _, element := range a.AssignedTo {
if element == paramId {
userList = append(userList, a)
}
}
}
if len(userList) == 0 {
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "user not found"})
return
}
c.IndentedJSON(http.StatusOK, userList)
CodePudding user response:
Looks like your return statement is inside your for loop, so it find the first value and immediately returns. Your function doesn't return when you comment it out so it completes successfully and then prints the error message. Move the return statement outside both for loops and it should be fine. Also, add logic for sending the error message by having an errorflag. The alternative solution from Manjeet Thakur is also recommended.
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)
}
var errorflag := 1
for _, a := range newUsers {
for _, element := range a.AssignedTo {
if element == paramId {
c.IndentedJSON(http.StatusOK, a)
errorflag := 0
//if I comment this line line then I geting message `"user not found"` along with results
}
}
}
if errorflag{
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "user not found"})
}
return
}
CodePudding user response:
This is working perfectly for me.
var userList []models.User
for _, a := range newUsers {
for _, element := range a.AssignedTo {
if element == paramId {
userList = append(userList, a)
}
}
}
if len(userList) == 0 {
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "user not found"})
return
}
c.IndentedJSON(http.StatusOK, userList)