我有 table 个用户。我想写一个 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
我有 table 个用户。我想写一个 API 来使用 id 抓取数据。我不断收到以下错误。请注意,数据库已经满了。我想创建一个 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
您没有设置 package-level db
变量,因为您使用 :=
.
并在出现错误时退出 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())
}
}
我有 table 个用户。我想写一个 API 来使用 id 抓取数据。我不断收到以下错误。请注意,数据库已经满了。我想创建一个 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
您没有设置 package-level db
变量,因为您使用 :=
.
并在出现错误时退出 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())
}
}