登录
首页 >  Golang >  Go问答

遇到数据库已满的情况如何解决:编写API使用ID获取用户数据

来源:stackoverflow

时间:2024-03-22 18:18:35 253浏览 收藏

当数据库已满时,尝试使用 ID 获取用户数据时,可能会遇到错误。此问题可能是由于包级 db 变量未正确设置造成的。要解决此问题,请在 main 函数中使用 var db *gorm.DB 声明包级 db 变量,并使用 := 赋值运算符。此外,如果出现错误,请退出 main 函数以防止继续使用 nil db。

问题内容

我有一个用户表。我想编写一个使用 id 获取数据的 api。我不断收到以下错误。请注意,数据库已经满了。我想创建一个 get api,在其中传递 id 并获取该行作为结果。我尝试了一些方法,但我不断收到此错误。 数据库架构-

deadpool=# \d user_data
                                           table "public.user_data"
       column       |           type           | collation | nullable |                default
--------------------+--------------------------+-----------+----------+---------------------------------------
 id                 | integer                  |           | not null | nextval('user_data_id_seq'::regclass)
 created_at         | timestamp with time zone |           |          |
 updated_at         | timestamp with time zone |           |          |
 deleted_at         | timestamp with time zone |           |          |
 name               | text                     |           |          |
 age                | text                     |           |          |
 gender             | text                     |           |          |
 party_code         | text                     |           |          |
 criminal_cases     | text                     |           |          |
 number_of_cases    | text                     |           |          |
 serious_ipc_counts | text                     |           |          |
 ipc_details        | text                     |           |          |
 education_level    | text                     |           |          |
 movable_assets     | text                     |           |          |
 immovable_assets   | text                     |           |          |
 total_assets       | text                     |           |          |
 total_liabilities  | text                     |           |          |
 pan_given          | text                     |           |          |
 election           | text                     |           |          |
 constituency       | text                     |           |          |
indexes:
    "user_data_pkey" primary key, btree (id)
    "idx_user_data_deleted_at" btree (deleted_at)

我的代码- 包主要

import (
    "encoding/json"
    "fmt"
    "github.com/gorilla/mux"
    "github.com/jinzhu/gorm"
    _ "github.com/jinzhu/gorm/dialects/postgres"
    "log"
    "net/http"
)
var db *gorm.db
var err error
type userdata struct {
    gorm.model
    name               string `json:"name"`
    age                string `json:"age"`
    gender             string `json:"gender"`
    party_code         string `json:"party_code"`
    criminal_cases     string `json:"criminal_cases"`
    number_of_cases    string `json:"number_of_cases"`
    serious_ipc_counts string `json:"serious_ipc_counts"`
    ipc_details        string `json:"ipc_details"`
    education_level    string `json:"education_level"`
    movable_assets     string `json:"movable_assets"`
    immovable_assets   string `json:"immovable_assets"`
    total_assets       string `json:"total_assets"`
    total_liabilities  string `json:"total_liabilities"`
    pan_given          string `json:"pan_given"`
    election           string `json:"election"`
    constituency       string `json:"constituency"`
}
func main() {
    // note: see we’re using = to assign the global var
    // instead of := which would assign it only in this function
    db, err := gorm.open("postgres", "host=localhost port=5432 dbname=deadpool sslmode=disable")
    if err != nil {
        fmt.println(err)
    }
    defer db.close()
    router := mux.newrouter()
    router.handlefunc("/resources/{id}", getresource).methods("get")
    log.fatal(http.listenandserve(":8080", router))
}

func getresource(w http.responsewriter, r *http.request) {
    params := mux.vars(r)
    var userdata userdata
    db.first(&userdata, params["id"])
    json.newencoder(w).encode(&userdata)
}

我面临的错误-

> 2019/06/13 18:41:22 http: panic serving [::1]:56779: runtime error:
> invalid memory address or nil pointer dereference goroutine 47
> [running]: net/http.(*conn).serve.func1(0xc00020e460)
>         /usr/local/Cellar/go/1.11.4/libexec/src/net/http/server.go:1746 +0xd0
> panic(0x13ba240, 0x1714f30)
>         /usr/local/Cellar/go/1.11.4/libexec/src/runtime/panic.go:513 +0x1b9 github.com/jinzhu/gorm.(*DB).clone(0x0, 0x10)
>         /Users/abhisekroy/go/src/github.com/jinzhu/gorm/main.go:768 +0x26 github.com/jinzhu/gorm.(*DB).NewScope(0x0, 0x1384740, 0xc000244500, 0x0)
>         /Users/abhisekroy/go/src/github.com/jinzhu/gorm/main.go:179 +0x2f github.com/jinzhu/gorm.(*DB).First(0x0, 0x1384740, 0xc000244500, 0xc00018a830, 0x1, 0x1, 0x12cdeda)
>         /Users/abhisekroy/go/src/github.com/jinzhu/gorm/main.go:299 +0x5a main.GetResource(0x1490560, 0xc000240380, 0xc000189600)
>         /Users/abhisekroy/go/src/github.com/CreditSaisonIndia/deadpool/ExternalVerifications/externalVerifications.go:49
> +0x13d net/http.HandlerFunc.ServeHTTP(0x143ba08, 0x1490560, 0xc000240380, 0xc000189600)
>         /usr/local/Cellar/go/1.11.4/libexec/src/net/http/server.go:1964 +0x44
> github.com/gorilla/mux.(*Router).ServeHTTP(0xc0000ec480, 0x1490560,
> 0xc000240380, 0xc00022b200)
>         /Users/abhisekroy/go/src/github.com/gorilla/mux/mux.go:212 +0xd0 net/http.serverHandler.ServeHTTP(0xc000087520, 0x1490560, 0xc000240380, 0xc00022b200)
>         /usr/local/Cellar/go/1.11.4/libexec/src/net/http/server.go:2741 +0xab
> net/http.(*conn).serve(0xc00020e460, 0x14908a0, 0xc000224880)
>         /usr/local/Cellar/go/1.11.4/libexec/src/net/http/server.go:1847 +0x646
> created by net/http.(*Server).Serve
>         /usr/local/Cellar/go/1.11.4/libexec/src/net/http/server.go:2851 +0x2f5
> 2019/06/13 18:41:22 http: panic serving [::1]:56780: runtime error:
> invalid memory address or nil pointer dereference goroutine 48
> [running]: net/http.(*conn).serve.func1(0xc00020e500)
>         /usr/local/Cellar/go/1.11.4/libexec/src/net/http/server.go:1746 +0xd0
> panic(0x13ba240, 0x1714f30)
>         /usr/local/Cellar/go/1.11.4/libexec/src/runtime/panic.go:513 +0x1b9 github.com/jinzhu/gorm.(*DB).clone(0x0, 0x10)
>         /Users/abhisekroy/go/src/github.com/jinzhu/gorm/main.go:768 +0x26 github.com/jinzhu/gorm.(*DB).NewScope(0x0, 0x1384740, 0xc00026c780, 0x0)
>         /Users/abhisekroy/go/src/github.com/jinzhu/gorm/main.go:179 +0x2f github.com/jinzhu/gorm.(*DB).First(0x0, 0x1384740, 0xc00026c780, 0xc000226250, 0x1, 0x1, 0x12cdeda)
>         /Users/abhisekroy/go/src/github.com/jinzhu/gorm/main.go:299 +0x5a main.GetResource(0x1490560, 0xc000266540, 0xc00022b600)
>         /Users/abhisekroy/go/src/github.com/CreditSaisonIndia/deadpool/ExternalVerifications/externalVerifications.go:49
> +0x13d net/http.HandlerFunc.ServeHTTP(0x143ba08, 0x1490560, 0xc000266540, 0xc00022b600)
>         /usr/local/Cellar/go/1.11.4/libexec/src/net/http/server.go:1964 +0x44
> github.com/gorilla/mux.(*Router).ServeHTTP(0xc0000ec480, 0x1490560,
> 0xc000266540, 0xc00022b400)
>         /Users/abhisekroy/go/src/github.com/gorilla/mux/mux.go:212 +0xd0 net/http.serverHandler.ServeHTTP(0xc000087520, 0x1490560, 0xc000266540, 0xc00022b400)
>         /usr/local/Cellar/go/1.11.4/libexec/src/net/http/server.go:2741 +0xab
> net/http.(*conn).serve(0xc00020e500, 0x14908a0, 0xc000224980)
>         /usr/local/Cellar/go/1.11.4/libexec/src/net/http/server.go:1847 +0x646
> created by net/http.(*Server).Serve
>         /usr/local/Cellar/go/1.11.4/libexec/src/net/http/server.go:2851 +0x2f5

解决方案


您没有设置包级 db var,因为您使用 :=

如果出现错误,请退出 main 函数,这样您就不会继续使用 nil db。

var db *gorm.DB

func main() {
    var err error
    db, err = gorm.Open()
    if err != nil {
        log.Fatalf("unable to connect to database: %s", err.Error())
    }
}

今天关于《遇到数据库已满的情况如何解决:编写API使用ID获取用户数据》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!

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