0

I'm trying to build an API endpoint using Revel for Go.

My models/models.go looks like this -

type Category struct {
Name string        `bson:"name"`
Slug  string        `bson:"slug"`}

func GetCategories(s *mgo.Session) *Category {
var results []Category
Collection(s).Find(nil).All(&results)
return results}

My controllers/book.go looks like this -

type Category struct {
*revel.Controller
revelbasic.MongoController}

func (c Category) Categories() revel.Result {
b := models.GetCategories(c.MongoSession)

return c.RenderJson(b)}

I've configured my conf/routes like this -

GET /categories Book.Categories

When I run the code, I get this error -

cannot use results (type []Category) as type *Category in return argument

I understand that I'm doing something wrong with the database query. Please help!

Gaurav Ojha
  • 1,147
  • 1
  • 15
  • 37

1 Answers1

0

The error in your code is because of type mismatch between function GetCategories's return value declaration and what you are actually returning. To fix, change the return type to return a slice of results:

func GetCategories(s *mgo.Session) []Category {
    var results []Category
    Collection(s).Find(nil).All(&results)
    return results
}
abhink
  • 8,740
  • 1
  • 36
  • 48