douyamitong57935 2019-06-13 13:16
浏览 51
已采纳

我有一个用户表。 我想编写一个API,它将使用id捕获数据。 我不断收到以下错误。 提醒您数据库已满

I have a table of users. I want to write an API that will grab data using id. I keep getting the below error. Mind you the database is already full. I want to create a get api where I pass the id and get that row as a result. I have tried a few things but I keep getting this error. DB Schema-

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)

My code- package main

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)
}

The error that I am facing-

> 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
  • 写回答

1条回答 默认 最新

  • douzhengzuo7283 2019-06-13 14:22
    关注

    You're not setting package-level db var, because you use :=.

    And exit from the main func in case of error, so you don't continue to work with 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())
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据