im trying to create an rest api with go/echo and postgres with raw sql but i cant make it work, no idea whats the problem

the console prints the text in the title

recipe.go

func CreateRecipe(recipe *Recipe) error {     
    query := `INSERT INTO recipes(title, ingredients, description) VALUES($1, $2, $3);`
    _, err := db.Exec(query, recipe.Title, recipe.Ingredients, recipe.Description)
    if err != nil {         
        return err     
    }      
    return nil 
}  

router.go

func PostRecipe(c echo.Context) error {
    recipe := new(models.Recipe) 

    if err := c.Bind(recipe); err != nil {
        return err
    }

    err := models.CreateRecipe(recipe)

    if err != nil {
        return err
    }
    
    return c.JSON(http.StatusCreated, recipe)
}

server.go

func Start() {
    //Setting up echo 
    e := echo.New()

    e.Use(middleware.CORS())
    
    e.GET("/api/recipes", Home)

    e.POST("/api/recipes", PostRecipe)

    e.Logger.Fatal(e.Start(":4000"))
}

enter image description here

1

There are 1 answers

0
vfreis09 On BEST ANSWER

I initialize the db in the wrong way, I used

db, err := sql.Open("postgres", dbinfo)

instead of

db, err = sql.Open("postgres", dbinfo)