我更新了gin-gonic并希望用它来保存表单。
这是我的代码:
import (
"fmt"
"github.com/gin-gonic/contrib/sessions"
"github.com/gin-gonic/gin"
...
func HandlePost(c *gin.Context) {
userId, userName := getUserId(c)
title := c.PostForm("title")
content := c.PostForm("content")
fmt.Println("userId is:", userId) //ok
fmt.Println("userName is: ", userName) //ok
fmt.Println("title is:", title) //empty
fmt.Println("content is: ", content) //empty
...
我使用Postman发布表单,如下:
我没有收到任何错误,但收到的表单字段为空。所以想知道这里有什么问题以及如何解决它?
更新:以下是我实施getUserId
的方式:
func getUserId(c *gin.Context) (int, string) {
var user []model.User
session := sessions.Default(c)
userEmail := session.Get("user-id").(string)
if userEmail == "" {
fmt.Println("userEmail NOT found")
} else {
err := shared.Dbmap.Select(&user, "SELECT * FROM user WHERE email = ? LIMIT 1", userEmail)
if err != nil {
log.Panic(err)
}
}
return user[0].Id, user[0].UserName
}