postgresqlgogo-gorm

cannot use r.GetBooks (value of type func(context *fiber.Ctx) error) as func(*fiber.Ctx) value in argument to api.Get


I tried to make an API with golang and postgres using gorm framework.I got error that said cannot use r.GetBooks (value of type func(context *fiber.Ctx) error) as func(*fiber.Ctx) value in argument to api.Get

Here's my code:

import (
    "fmt"
    "log"
    "os"

    "net/http"

    "github.com/gofiber/fiber"
    "github.com/joho/godotenv"
    "gorm.io/gorm"
)


func (r *Repository) GetBooks(context *fiber.Ctx) error {
    bookModels := &[]models.Books{}

    err := r.DB.Find(bookModels).Error
    if err != nil {
        context.Status(http.StatusBadRequest).JSON(
            &fiber.Map{"message": "Failed to get the book"})
        return err
    }

    context.Status(http.StatusOK).JSON(
        &fiber.Map{
            "message": "Books fetched successfully",
            "data": bookModels,
        })

    return nil
}
func (r *Repository) SetupRoutes(app *fiber.App){
    
    api := app.Group("/api")

    api.Get("/books", r.GetBooks)
}

I already return nil at the end of the function and use error as a return for the function. But still didn't work


Solution

  • You are using an old version of the fiber framework from 2020.

    Import "github.com/gofiber/fiber/v2" (or the new v3 which is in beta) to get the handler definition of func(Ctx) error.