尝试使用Fiber和GORM编写一个创建数据库的POST请求,但是出现了错误。

huangapple go评论119阅读模式
英文:

Trying to write a post request to create a database with fiber and gorm and it gives me an error

问题

尝试使用gorm作为数据库和fiber作为golang框架来输出一个POST请求,但似乎有时候不起作用,有时候返回"json: unmarshal(non-pointer, main.AcctDetails)"的错误。请有人帮我解决这个问题,我已经卡在这里很长时间了。

  1. package main
  2. import (
  3. "github.com/gofiber/fiber/v2"
  4. "gorm.io/driver/sqlite"
  5. "gorm.io/gorm"
  6. )
  7. var (
  8. DB *gorm.DB
  9. err error
  10. )
  11. type AcctDetails struct {
  12. gorm.Model
  13. ID uint `json:"id"`
  14. AcctName string `json:"acctname"`
  15. AcctNumber string `json:"acctnumber"`
  16. UsersPhoneNo string `json:"usersphoneno"`
  17. }
  18. func Routers(app *fiber.App) {
  19. app.Post("/bank", CreateUser)
  20. }
  21. func CreateUser(c *fiber.Ctx) error {
  22. info := new(AcctDetails)
  23. if err := c.BodyParser(info); err != nil {
  24. return c.Status(500).SendString(err.Error())
  25. }
  26. DB.Create(&info)
  27. return c.Status(200).JSON(&info)
  28. }
  29. func main() {
  30. app := fiber.New()
  31. db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
  32. if err != nil {
  33. panic("Cannot Connect To Database")
  34. }
  35. DB = db
  36. DB.AutoMigrate(&AcctDetails{})
  37. Routers(app)
  38. app.Listen(":3000")
  39. }

它返回以下错误:

  1. PS C:\Users\Chinonso\Desktop\New folder\GO\main> go run main.go
  2. 2022/04/29 22:47:51 C:/Users/Chinonso/Desktop/New folder/GO/main/main.go:45 SLOW SQL >= 200ms
  3. [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`))
  4. *this is after the server starts*
  5. panic: runtime error: invalid memory address or nil pointer dereference
  6. [signal 0xc0000005 code=0x0 addr=0x0 pc=0x7ff7bc6f05a2]
  7. goroutine 8 [running]:
  8. gorm.io/gorm.(*DB).Create(0xc0000c0000, {0x7ff7bc92e820, 0xc0000b4010})
  9. C:/Users/Chinonso/go/pkg/mod/gorm.io/gorm@v1.23.5/finisher_api.go:18 +0x22
  10. main.CreateUser(0xc0000c0000)
  11. C:/Users/Chinonso/Desktop/New folder/GO/main/main.go:35 +0xe5
  12. github.com/gofiber/fiber/v2.(*App).next(0xc0000023c0, 0xc0000c0000)
  13. C:/Users/Chinonso/go/pkg/mod/github.com/gofiber/fiber/v2@v2.32.0/router.go:132 +0x1be
  14. github.com/gofiber/fiber/v2.(*App).handler(0xc0000023c0, 0x7ff7bc3a19b7)
  15. C:/Users/Chinonso/go/pkg/mod/github.com/gofiber/fiber/v2@v2.32.0/router.go:159 +0x45
  16. github.com/valyala/fasthttp.(*Server).serveConn(0xc000127440, {0x7ff7bca94f30, 0xc000006ef8})
  17. C:/Users/Chinonso/go/pkg/mod/github.com/valyala/fasthttp@v1.35.0/server.go:2338 +0x11ae
  18. github.com/valyala/fasthttp.(*workerPool).workerFunc(0xc0002a9360, 0xc0002bd440)
  19. C:/Users/Chinonso/go/pkg/mod/github.com/valyala/fasthttp@v1.35.0/workerpool.go:224 +0xa9
  20. github.com/valyala/fasthttp.(*workerPool).getCh.func1()
  21. C:/Users/Chinonso/go/pkg/mod/github.com/valyala/fasthttp@v1.35.0/workerpool.go:196 +0x38
  22. created by github.com/valyala/fasthttp.(*workerPool).getCh
  23. C:/Users/Chinonso/go/pkg/mod/github.com/valyala/fasthttp@v1.35.0/workerpool.go:195 +0x1b5
  24. exit status 2
  25. PS C:\Users\Chinonso\Desktop\New folder\GO\main>

请有人帮我解决这段代码,这个问题困扰了我两个月了。

英文:

trying to oupt a post (create) request using gorm as my database and fiber as my golang framewor but it seems like it is not working sometimes it returns "json: unmarshal(non-pointer, main.AcctDetails)" someone should please help me out i have been stuck here for quite a long time

  1. package main
  2. import (
  3. "github.com/gofiber/fiber/v2"
  4. "gorm.io/driver/sqlite"
  5. "gorm.io/gorm"
  6. )
  7. var (
  8. DB *gorm.DB
  9. err error
  10. )
  11. type AcctDetails struct {
  12. gorm.Model
  13. ID uint `json:"id"`
  14. AcctName string `json:"acctname"`
  15. AcctNumber string `json:"acctnumber"`
  16. UsersPhoneNo string `json:"usersphoneno"`
  17. }
  18. func Routers(app *fiber.App) {
  19. app.Post("/bank", CreateUser)
  20. }
  21. func CreateUser(c *fiber.Ctx) error {
  22. info := new(AcctDetails)
  23. if err := c.BodyParser(info); err != nil {
  24. return c.Status(500).SendString(err.Error())
  25. }
  26. DB.Create(&info)
  27. return c.Status(200).JSON(&info)
  28. }
  29. func main() {
  30. app := fiber.New()
  31. db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
  32. if err != nil {
  33. panic("Cannot Connect To Database")
  34. }
  35. db.AutoMigrate(&AcctDetails{})
  36. Routers(app)
  37. app.Listen(":3000")
  38. }

and it returns this as error

  1. PS C:\Users\Chinonso\Desktop\New folder\GO\main> go run main.go
  2. 2022/04/29 22:47:51 C:/Users/Chinonso/Desktop/New folder/GO/main/main.go:45 SLOW SQL >= 200ms
  3. [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`))
  4. *this is after the server starts*
  5. panic: runtime error: invalid memory address or nil pointer dereference
  6. [signal 0xc0000005 code=0x0 addr=0x0 pc=0x7ff7bc6f05a2]
  7. goroutine 8 [running]:
  8. gorm.io/gorm.(*DB).Create(0xc0000c0000, {0x7ff7bc92e820, 0xc0000b4010})
  9. C:/Users/Chinonso/go/pkg/mod/gorm.io/gorm@v1.23.5/finisher_api.go:18 +0x22
  10. main.CreateUser(0xc0000c0000)
  11. C:/Users/Chinonso/Desktop/New folder/GO/main/main.go:35 +0xe5
  12. github.com/gofiber/fiber/v2.(*App).next(0xc0000023c0, 0xc0000c0000)
  13. C:/Users/Chinonso/go/pkg/mod/github.com/gofiber/fiber/v2@v2.32.0/router.go:132 +0x1be
  14. github.com/gofiber/fiber/v2.(*App).handler(0xc0000023c0, 0x7ff7bc3a19b7)
  15. C:/Users/Chinonso/go/pkg/mod/github.com/gofiber/fiber/v2@v2.32.0/router.go:159 +0x45
  16. github.com/valyala/fasthttp.(*Server).serveConn(0xc000127440, {0x7ff7bca94f30, 0xc000006ef8})
  17. C:/Users/Chinonso/go/pkg/mod/github.com/valyala/fasthttp@v1.35.0/server.go:2338 +0x11ae
  18. github.com/valyala/fasthttp.(*workerPool).workerFunc(0xc0002a9360, 0xc0002bd440)
  19. C:/Users/Chinonso/go/pkg/mod/github.com/valyala/fasthttp@v1.35.0/workerpool.go:224 +0xa9
  20. github.com/valyala/fasthttp.(*workerPool).getCh.func1()
  21. C:/Users/Chinonso/go/pkg/mod/github.com/valyala/fasthttp@v1.35.0/workerpool.go:196 +0x38
  22. created by github.com/valyala/fasthttp.(*workerPool).getCh
  23. C:/Users/Chinonso/go/pkg/mod/github.com/valyala/fasthttp@v1.35.0/workerpool.go:195 +0x1b5
  24. exit status 2
  25. PS C:\Users\Chinonso\Desktop\New folder\GO\main>

please someone should help me resolve this code it's giving me headach since two months now.

答案1

得分: 1

你正在尝试访问DB.Create(),但没有将db赋值给全局变量DB

在之前的代码中:

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

修改后的代码如下:

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

You are trying to access DB.Create() but didn't assign db to global variable DB.
Note: using debugger could prevent a lot of headaches

Before

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

After

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

huangapple
  • 本文由 发表于 2022年4月30日 06:07:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/72064116.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定