运行时错误:golang 中无效的内存地址或零指针取消引用

问题描述 投票:0回答:0

在我的终端中它说错误在 D:/data lecture/Hacktive8/tesjwt/controllers/userControllers.go:26 (0xa2010f) UserRegister: 错误 := db.Debug().Create(&User).Error

并说运行时错误:无效的内存地址或零指针取消引用

这里是我的 userControllers.go

package controllers

import (
    "net/http"

    "github.com/gin-gonic/gin"
    "tesjwt.go/database"
    "tesjwt.go/helpers"
    "tesjwt.go/models"
)

var appJSON = "application/json"

func UserRegister(c *gin.Context) {
    db := database.GetDB()
    contentType := helpers.GetContentType(c)

    User := models.User{}

    if contentType == appJSON {
        c.ShouldBindJSON(&User)
    } else {
        c.ShouldBind(&User)
    }

    err := db.Debug().Create(&User).Error
    if err != nil {
        c.JSON(http.StatusBadRequest, gin.H{
            "error":   "Bad Request",
            "message": err.Error(),
        })
        return
    }

    c.JSON(http.StatusCreated, gin.H{
        "id":        User.ID,
        "email":     User.Email,
        "full_name": User.FullName,
    })
}

谁能帮帮我?

i want to post json with postman but i get this errors, can anyone help me?

go postman go-gorm go-gin
© www.soinside.com 2019 - 2024. All rights reserved.