尝试编写 post 请求以使用 fiber 和 gorm 创建数据库,但它给了我一个错误
Trying to write a post request to create a database with fiber and gorm and it gives me an error
尝试使用 gorm 作为我的数据库并使用 fiber 作为我的 golang 框架来发出 post(创建)请求,但它似乎有时无法正常工作 returns” 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")
}
它returns这是错误
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/gorm@v1.23.5/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/v2@v2.32.0/router.go:132 +0x1be
github.com/gofiber/fiber/v2.(*App).handler(0xc0000023c0, 0x7ff7bc3a19b7)
C:/Users/Chinonso/go/pkg/mod/github.com/gofiber/fiber/v2@v2.32.0/router.go:159 +0x45
github.com/valyala/fasthttp.(*Server).serveConn(0xc000127440, {0x7ff7bca94f30, 0xc000006ef8})
C:/Users/Chinonso/go/pkg/mod/github.com/valyala/fasthttp@v1.35.0/server.go:2338 +0x11ae
github.com/valyala/fasthttp.(*workerPool).workerFunc(0xc0002a9360, 0xc0002bd440)
C:/Users/Chinonso/go/pkg/mod/github.com/valyala/fasthttp@v1.35.0/workerpool.go:224 +0xa9
github.com/valyala/fasthttp.(*workerPool).getCh.func1()
C:/Users/Chinonso/go/pkg/mod/github.com/valyala/fasthttp@v1.35.0/workerpool.go:196 +0x38
created by github.com/valyala/fasthttp.(*workerPool).getCh
C:/Users/Chinonso/go/pkg/mod/github.com/valyala/fasthttp@v1.35.0/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{})
尝试使用 gorm 作为我的数据库并使用 fiber 作为我的 golang 框架来发出 post(创建)请求,但它似乎有时无法正常工作 returns” 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")
}
它returns这是错误
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/gorm@v1.23.5/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/v2@v2.32.0/router.go:132 +0x1be
github.com/gofiber/fiber/v2.(*App).handler(0xc0000023c0, 0x7ff7bc3a19b7)
C:/Users/Chinonso/go/pkg/mod/github.com/gofiber/fiber/v2@v2.32.0/router.go:159 +0x45
github.com/valyala/fasthttp.(*Server).serveConn(0xc000127440, {0x7ff7bca94f30, 0xc000006ef8})
C:/Users/Chinonso/go/pkg/mod/github.com/valyala/fasthttp@v1.35.0/server.go:2338 +0x11ae
github.com/valyala/fasthttp.(*workerPool).workerFunc(0xc0002a9360, 0xc0002bd440)
C:/Users/Chinonso/go/pkg/mod/github.com/valyala/fasthttp@v1.35.0/workerpool.go:224 +0xa9
github.com/valyala/fasthttp.(*workerPool).getCh.func1()
C:/Users/Chinonso/go/pkg/mod/github.com/valyala/fasthttp@v1.35.0/workerpool.go:196 +0x38
created by github.com/valyala/fasthttp.(*workerPool).getCh
C:/Users/Chinonso/go/pkg/mod/github.com/valyala/fasthttp@v1.35.0/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{})