K
K
kireke2021-08-15 19:36:04
go
kireke, 2021-08-15 19:36:04

Why am I only getting the first product from the gorm go table?

I have a function

func GetProducts(c *gin.Context) {
  var product models.Product
  database.DB.Find(&product)
  c.JSON(http.StatusOK, product)
}

should get everything from the products table through the Find method, but only the first product in the response, what should I do?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Mamonov, 2021-08-15
@kireke

Try like this

func GetProducts(c *gin.Context) {
  var products []models.Product
  database.DB.Find(&products)
  c.JSON(http.StatusOK, products)
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question