0

I Want to fetch all records in database By using NAME but, if I am using ALL it is showing 500 internal error but, if I Kept One(JSON) I am getting only one record. What is the solution to fetch all records by Name?

func (uc UserController) Filter(c *gin.Context) {
    var name = c.Params.ByName("Name")
    var json models.User
    err := c.Bind(&json)
    if err != nil {
        log.Fatal("error")
        return
    }
    json.Name = name
    fi := bson.D{{"Name", name}}
    err = uc.session.DB(DB_NAME).C(DB_COLLECTION).Find(fi).All(json)

    if err == nil {
        c.Writer.Header().Set("Content-Type", "application/json")
        c.JSON(201, &json)
    } else {
        c.JSON(500, gin.H{"result": "An error occured"})
    }

}

1 Answer 1

2

You should pass an array (var json []models.User) to the All(&json) function, but you're passing one item (var json models.User).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.