登录
首页 >  Golang >  Go问答

**请求编写一份指导创建使用 Fiber 和 Gorm 的数据库的帖子,并解决一个出现的错误**

来源:stackoverflow

时间:2024-02-21 16:12:35 154浏览 收藏

今日不肯埋头,明日何以抬头!每日一句努力自己的话哈哈~哈喽,今天我将给大家带来一篇《**请求编写一份指导创建使用 Fiber 和 Gorm 的数据库的帖子,并解决一个出现的错误**》,主要内容是讲解等等,感兴趣的朋友可以收藏或者有更好的建议在评论提出,我都会认真看的!大家一起进步,一起学习!

问题内容

尝试使用 gorm 作为我的数据库和 fiber 作为我的 golang 框架来发出发布(创建)请求,但它似乎不起作用,有时它会返回“json: unmarshal(non-pointer, main.acctdetails) “请有人帮助我,我已经被困在这里很长一段时间了

package main

import (

    "github.com/gofiber/fiber/v2"
    "gorm.io/driver/sqlite"
    "gorm.io/gorm"
)

var (
    db  *gorm.db
    err error
)

type acctdetails struct {
        gorm.model
        id           uint    `json:"id"`
        acctname     string  `json:"acctname"`
        acctnumber   string  `json:"acctnumber"`
        usersphoneno string  `json:"usersphoneno"`
    }


func routers(app *fiber.app) {
    app.post("/bank", createuser)
}

func createuser(c *fiber.ctx) error {
    info := new(acctdetails)
    
    if err := c.bodyparser(info); err != nil {
        return c.status(500).sendstring(err.error())
    }

    db.create(&info)
    return c.status(200).json(&info)
}

func main() {
    app := fiber.new()
    db, err := gorm.open(sqlite.open("test.db"), &gorm.config{})
    if err != nil {
        panic("cannot connect to database")
    }
    db.automigrate(&acctdetails{})
    routers(app)
    app.listen(":3000")
}

它返回此错误

PS C:\Users\Chinonso\Desktop\New folder\GO\main> go run main.go

2022/04/29 22:47:51 C:/Users/Chinonso/Desktop/New folder/GO/main/main.go:45 SLOW SQL >= 200ms
[272.120ms] [rows:0] CREATE TABLE `acct_details` (`id` integer,`created_at` datetime,`updated_at` datetime,`deleted_at` datetime,`acct_name` text,`acct_number` text,`users_phone_no` text,PRIMARY KEY (`id`))

*this is after the server starts*

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x0 pc=0x7ff7bc6f05a2]

goroutine 8 [running]:
gorm.io/gorm.(*DB).Create(0xc0000c0000, {0x7ff7bc92e820, 0xc0000b4010})
        C:/Users/Chinonso/go/pkg/mod/gorm.io/[email protected]/finisher_api.go:18 +0x22
main.CreateUser(0xc0000c0000)
        C:/Users/Chinonso/Desktop/New folder/GO/main/main.go:35 +0xe5
github.com/gofiber/fiber/v2.(*App).next(0xc0000023c0, 0xc0000c0000)
        C:/Users/Chinonso/go/pkg/mod/github.com/gofiber/fiber/[email protected]/router.go:132 +0x1be
github.com/gofiber/fiber/v2.(*App).handler(0xc0000023c0, 0x7ff7bc3a19b7)
        C:/Users/Chinonso/go/pkg/mod/github.com/gofiber/fiber/[email protected]/router.go:159 +0x45
github.com/valyala/fasthttp.(*Server).serveConn(0xc000127440, {0x7ff7bca94f30, 0xc000006ef8})
        C:/Users/Chinonso/go/pkg/mod/github.com/valyala/[email protected]/server.go:2338 +0x11ae
github.com/valyala/fasthttp.(*workerPool).workerFunc(0xc0002a9360, 0xc0002bd440)
        C:/Users/Chinonso/go/pkg/mod/github.com/valyala/[email protected]/workerpool.go:224 +0xa9
github.com/valyala/fasthttp.(*workerPool).getCh.func1()
        C:/Users/Chinonso/go/pkg/mod/github.com/valyala/[email protected]/workerpool.go:196 +0x38
created by github.com/valyala/fasthttp.(*workerPool).getCh
        C:/Users/Chinonso/go/pkg/mod/github.com/valyala/[email protected]/workerpool.go:195 +0x1b5
exit status 2
PS C:\Users\Chinonso\Desktop\New folder\GO\main>

请有人帮我解决这个代码,两个月以来它一直让我头疼。


正确答案


您正在尝试访问 db.create(),但未将 db 分配给全局变量 db
注意:使用调试器可以避免很多麻烦

之前

    db, err := gorm.open(sqlite.open("test.db"), &gorm.config{})

之后

    DB, err = gorm.Open(sqlite.Open("test.db"), &gorm.Config{})

今天关于《**请求编写一份指导创建使用 Fiber 和 Gorm 的数据库的帖子,并解决一个出现的错误**》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>