3

我是新来的golanggorm我正在努力了解它是如何FirstOrCreate工作的。我有模型:

type Setting struct {
    Id        int64
    Email     string
    Phone     string
    Skype     string
    Country   string
    City      string
    Address   string
    CreatedAt time.Time
    UpdatedAt time.Time
    DeletedAt time.Time
}

和下一个片段:

func (c Admins) ShowSettings() revel.Result {    
    var setting models.Setting
    DB.FirstOrCreate(&setting, models.Setting{})

    return c.Render(setting)
}

第一次运行这个函数时,一切正常,它必须在数据库中创建新实体,但是当我下次调用这个函数来显示这个时,setting我有错误:

runtime error: invalid memory address or nil pointer dereference

错在哪里?

编辑: 完全错误:

ERROR 2014/07/11 08:16:34 panic.go:29: runtime error: invalid memory address or nil pointer dereference
/home/cnaize/Dropbox/develop/gocode/src/advorts/app/controllers/admins.go:99 (0x472bdf)
    Admins.ShowSettings: DB.FirstOrCreate(&setting, models.Setting{})
/home/cnaize/Dropbox/develop/gocode/src/advorts/app/controllers/admins.go:1 (0x4765ce)
    (*Admins).ShowSettings: package controllers
/usr/lib/go/src/pkg/runtime/asm_amd64.s:339 (0x426e62)
    call32: CALLFN(call32, 32)
/usr/lib/go/src/pkg/reflect/value.go:474 (0x43488b)
    Value.call: call(fn, ptr, uint32(size))
/usr/lib/go/src/pkg/reflect/value.go:345 (0x43397d)
    Value.Call: return v.call("Call", in)
/home/cnaize/Dropbox/develop/gocode/src/github.com/revel/revel/invoker.go:35 (0x451580)
    com/revel/revel.ActionInvoker: resultValue = methodValue.Call(methodArgs)[0]
/home/cnaize/Dropbox/develop/gocode/src/github.com/revel/revel/compress.go:45 (0x44699c)
    com/revel/revel.CompressFilter: fc[0](c, fc[1:])
/home/cnaize/Dropbox/develop/gocode/src/github.com/revel/revel/intercept.go:103 (0x4500b0)
    com/revel/revel.InterceptorFilter: fc[0](c, fc[1:])
/home/cnaize/Dropbox/develop/gocode/src/advorts/app/init.go:37 (0x471842)
    func.001: fc[0](c, fc[1:]) // Execute the next filter stage.
/home/cnaize/Dropbox/develop/gocode/src/github.com/revel/revel/i18n.go:155 (0x44f70b)
    com/revel/revel.I18nFilter: fc[0](c, fc[1:])
/home/cnaize/Dropbox/develop/gocode/src/github.com/revel/revel/validation.go:191 (0x4637d6)
    com/revel/revel.ValidationFilter: fc[0](c, fc[1:])
/home/cnaize/Dropbox/develop/gocode/src/github.com/revel/revel/flash.go:45 (0x44cbbd)
    com/revel/revel.FlashFilter: fc[0](c, fc[1:])
/home/cnaize/Dropbox/develop/gocode/src/github.com/revel/revel/session.go:144 (0x45e3d3)
    com/revel/revel.SessionFilter: fc[0](c, fc[1:])
/home/cnaize/Dropbox/develop/gocode/src/github.com/revel/revel/params.go:133 (0x452e10)
    com/revel/revel.ParamsFilter: fc[0](c, fc[1:])
/home/cnaize/Dropbox/develop/gocode/src/github.com/revel/revel/filterconfig.go:208 (0x44c87e)
    com/revel/revel.FilterConfiguringFilter: fc[0](c, fc[1:])
/home/cnaize/Dropbox/develop/gocode/src/github.com/revel/revel/router.go:465 (0x45c456)
    com/revel/revel.RouterFilter: fc[0](c, fc[1:])
/home/cnaize/Dropbox/develop/gocode/src/github.com/revel/revel/panic.go:15 (0x45190d)
    com/revel/revel.PanicFilter: fc[0](c, fc[1:])
/home/cnaize/Dropbox/develop/gocode/src/github.com/revel/revel/watcher.go:160 (0x468201)
    com/revel/revel.func.029: fc[0](c, fc[1:])
/home/cnaize/Dropbox/develop/gocode/src/github.com/revel/revel/server.go:42 (0x45ca6e)
    com/revel/revel.handleInternal: Filters[0](c, Filters[1:])
/home/cnaize/Dropbox/develop/gocode/src/github.com/revel/revel/server.go:30 (0x45c7b4)
    com/revel/revel.handle: handleInternal(w, r, nil)
/usr/lib/go/src/pkg/net/http/server.go:1220 (0x4f0920)
    HandlerFunc.ServeHTTP: f(w, r)
/usr/lib/go/src/pkg/net/http/server.go:1597 (0x4f256e)
    serverHandler.ServeHTTP: handler.ServeHTTP(rw, req)
/usr/lib/go/src/pkg/net/http/server.go:1167 (0x4f0577)
    (*conn).serve: serverHandler{c.server}.ServeHTTP(w, w.req)
/usr/lib/go/src/pkg/runtime/proc.c:1394 (0x41a650)
    goexit: runtime·goexit(void)
4

1 回答 1

0

完整的错误非常清楚:

ERROR 2014/07/11 08:16:34 panic.go:29: runtime error: invalid memory address or nil pointer dereference
/home/cnaize/Dropbox/develop/gocode/src/advorts/app/controllers/admins.go:99 (0x472bdf)
    Admins.ShowSettings: DB.FirstOrCreate(&setting, models.Setting{})

您的“数据库”为 nil 在尝试您的 FirstOrCreate 之前启动数据库连接

db, err := gorm.Open("sqlite3", "test.db")
if err != nil {
    panic("failed to connect database")
}
defer db.Close()

// then you code
db.FirstOrCreate(&setting, models.Setting{})
...

或者,也许您的“数据库”是您忘记在应用启动时初始化的单例

于 2020-02-12T08:54:34.233 回答