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

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

Gorm - 不獲取數(shù)據(jù)

Gorm - 不獲取數(shù)據(jù)

Go
慕碼人2483693 2022-08-15 16:20:27
我正在嘗試使用Gin和Gorm設(shè)置REST API。以下是我的:main.gopackage mainimport (    "app/config"    "app/service"    "github.com/gin-gonic/gin")func main() {    r := gin.Default()    // Connect to database    config.ConnectDatabase()    r.GET("/books", service.FindBooks)    // Run the server    r.Run()}在我的,我試圖像這樣連接到DBconfig/setup.gopackage configimport (    "github.com/jinzhu/gorm"    _ "github.com/jinzhu/gorm/dialects/mysql")var DB *gorm.DB// ConnectDatabase : Setup connection to databasefunc ConnectDatabase() {    database, err := gorm.Open("mysql", "root:password@tcp(127.0.0.1:3306)/test_db")    database.SingularTable(true)    if err != nil {        panic("Failed to connect to database!")    }    DB = database}在我的中,我有以下邏輯:service/book.gopackage serviceimport (    "errors"    "app/config"    "app/model"    "fmt"    "net/http"    "github.com/gin-gonic/gin")// FindBooks : Get all booksfunc FindBooks(c *gin.Context) {    c.JSON(http.StatusOK, gin.H{"data": FindAllBooks()})}// FindAllBooks : Fetch all book from DBfunc FindAllBooks() []model.Book {    var book []model.Book    config.DB.Find(&book)    for i := 0; i < len(book); i++ {        fmt.Println(book[i])    }    return book}而我的定義如下:model/Book.gopackage modeltype Book struct {    id        int64  `json:"id" gorm:"primary_key"`    label     string `json:"label" gorm:"type:varchar(255)"`}當(dāng)我使用 運(yùn)行應(yīng)用程序時(shí),以下是我可以看到的日志:go run main.go[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production. - using env:   export GIN_MODE=release - using code:  gin.SetMode(gin.ReleaseMode)[GIN-debug] GET    /books                    --> feedconsumer/service.FindBooks (3 handlers)[GIN-debug] Environment variable PORT is undefined. Using port :8080 by default[GIN-debug] Listening and serving HTTP on :8080{0   }[GIN] 2021/03/09 - 12:43:43 | 200 |    2.313864ms |             ::1 | GET      "/books"基本上,這意味著對象實(shí)際上沒有被獲取。我在這里錯(cuò)過了什么?{0   }
查看完整描述

2 回答

?
守著一只汪

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

GORM用于定義模型字段。鏈接到文檔snake_case


因此,對于您的情況,它基本上是:


package model


type Book struct {

    ID        int64  `json:"id" gorm:"primary_key"`

    Label     string `json:"label" gorm:"type:varchar(255)"`

}


查看完整回答
反對 回復(fù) 2022-08-15
?
慕慕森

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

我想你可以像 https://gorm.io/docs/logger.html 一樣為GORM配置記錄器,以便能夠在這些日志中看到實(shí)際的數(shù)據(jù)庫查詢。乍一看,您正在以正確的方式完成所有事情,如 https://gorm.io/docs/query.html#Retrieving-all-objects 但我個(gè)人遇到了一些不可預(yù)測的GORM行為,這在文檔中并不那么明顯


查看完整回答
反對 回復(fù) 2022-08-15
  • 2 回答
  • 0 關(guān)注
  • 170 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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