简体   繁体   中英

gorm db.find(&users) to json with gin in golang

This is my GET Method the problem is that all i get in the json is one user instead there are 3 users in my database.

func GetUsers(c *gin.Context) {

var users = db.Find(&models.Person{})
c.JSON(200, users)
}

Try this:

func GetUsers(c *gin.Context) {
 users := []models.Person{}
 db.Find(&users)
 c.JSON(200, &users)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM