第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

運(yùn)行時(shí)錯(cuò)誤:POST 請(qǐng)求的無(wú)效內(nèi)存地址或 nil 指針取消引用(golang-gorm)

運(yùn)行時(shí)錯(cuò)誤:POST 請(qǐng)求的無(wú)效內(nèi)存地址或 nil 指針取消引用(golang-gorm)

Go
溫溫醬 2022-06-06 15:35:11
//main.gopackage mainimport (    "TestGoProject/src/Mappings"    "TestGoProject/src/Models"    _ "github.com/gin-gonic/gin"    "github.com/jinzhu/gorm"    _ "github.com/jinzhu/gorm/dialects/mysql"    _ "net/http")var db *gorm.DBfunc main()  {    initDb()    Mappings.InitializeRoutes()}func initDb() {    var err error    db, err = gorm.Open("mysql", "root:helloworld@/testapp2?charset=utf8&parseTime=True&loc=Local")    if err != nil {        panic("failed to connect database")    }    db.AutoMigrate(&Models.BookModel{})}//router.gopackage Mappingsimport (    "TestGoProject/src/Controllers"    "github.com/gin-gonic/gin")func InitializeRoutes()  {    router := gin.Default()    router.GET("/", Controllers.Index)    router.POST("/createBook", Controllers.Create)    _ = router.Run()}//BookController.gopackage Controllersimport (    "TestGoProject/src/Models"    "github.com/gin-gonic/gin"    "github.com/jinzhu/gorm"    _ "github.com/jinzhu/gorm/dialects/mysql"    "net/http")var db = gorm.DB{}func Index(c *gin.Context)  {    c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "message": "Im here!"})}func Create(c *gin.Context)  {    book := Models.BookModel{        Title: c.PostForm("title"),        Author: c.PostForm("author"),    }    db.Create(&book)    c.JSON(http.StatusCreated, gin.H{"status": http.StatusCreated, "message": "Book created successfully!", "bookId": book.ID})}//bookModel.gopackage Modelsimport "github.com/jinzhu/gorm"type BookModel struct {    gorm.Model    Title string `json:"title"`    Author string `json:"author"`}因此,當(dāng)我從 Postman 發(fā)送 POST 請(qǐng)求時(shí),收到這樣的錯(cuò)誤嘗試解決了很多方法,但沒(méi)有奏效。任何幫助都會(huì)很棒。TIA2020/05/25 10:54:50 [Recovery] 2020/05/25 - 10:54:50 panic recovered:POST /createBook HTTP/1.1Host: localhost:8080Accept: */*Accept-Encoding: gzip, deflate, brCache-Control: no-cacheConnection: keep-aliveContent-Length: 48Content-Type: application/jsonUser-Agent: PostmanRuntime/7.25.0
查看完整描述

1 回答

?
阿晨1998

TA貢獻(xiàn)2037條經(jīng)驗(yàn) 獲得超6個(gè)贊

您db的變量沒(méi)有為處理程序正確設(shè)置


設(shè)置db在gin.Context


func InitializeRoutes(db *gorm.DB) {

    router := gin.Default()

    // Provide db variable to controllers

    router.Use(func(c *gin.Context) {

        c.Set("db", db)

        c.Next()

    })

    ...

}

從 main 發(fā)送 db


Mappings.InitializeRoutes(db)

并從上下文中獲取在處理程序中使用


func Create(c *gin.Context) {

    db := c.MustGet("db").(*gorm.DB)

    ...

}

操場(chǎng)上的工作代碼在這里


查看完整回答
反對(duì) 回復(fù) 2022-06-06
  • 1 回答
  • 0 關(guān)注
  • 151 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)