- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是新手,尝试创建登录功能,尝试从数据库查询行时出现此错误:runtime error: invalid memory address or nil pointer dere
ference
此行导致的崩溃:
result := db.QueryRow("SELECT password FROM users WHERE email=$1", credentials.Email)
type Credentials struct {
Email string `json:"email", db:"email"`
Password string `json:"password", db:"password"`
}
func SignIn(w http.ResponseWriter, r *http.Request) {
credentials := &Credentials{}
err := json.NewDecoder(r.Body).Decode(credentials)
if err != nil {
// If there is something wrong with the request body, return a 400 status
w.WriteHeader(http.StatusBadRequest)
return
}
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+"dbname=%s sslmode=disable", dbInfo.Host, dbInfo.Port, dbInfo.User, dbInfo.DBname)
db, err := sql.Open("postgres", psqlInfo)
println(credentials.Email)
result := db.QueryRow("SELECT password FROM users WHERE email=$1", credentials.Email)
defer db.Close()
if err != nil {
//If there is an issue with the database, return a 500 error.
w.WriteHeader(http.StatusInternalServerError)
return
}
//We create another instance of 'Credentials' t store the credentials we get from the database
storedCreds := &Credentials{}
// Store the obtained password in `storedCreds`
err = result.Scan(&storedCreds.Password)
if err != nil {
// If an entry with the email does not exist, send an "Unauthorized"(401) status
if err == sql.ErrNoRows {
w.WriteHeader(http.StatusUnauthorized)
return
}
//If the error is of any other type, send a 500 status
w.WriteHeader(http.StatusInternalServerError)
return
}
if credentials.Password != storedCreds.Password {
//The two passwords does not match, return a 401 status
w.WriteHeader(http.StatusUnauthorized)
}
}
最佳答案
db, err := sql.Open("postgres", psqlInfo)
// ...
result := db.QueryRow(...)
db
可能是
nil
,因为
sql.Open
失败。
err != nil
类型时始终应该检查的那样检查
error
。
关于go - 无效的内存地址或QueryRow上的nil指针取消引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60115707/
我有一排这样的: 现在我想检索 签到时间和 结帐时间 : 好的,我已经得到了正确的查询。现在通过 Go 代码运行查询: package controllers import ( "absens
我有一个名为user.go的文件,该函数GetUserByUsernameOrEmail返回用户模型和错误。它从数据库中获取此用户信息。 我的目标是获取用户SELECT的所有参数,但返回的参数比应有的
beego Raw().QueryRows()的映射规则是什么这是我使用的结构: type ProcessingNetworkDataProviderConfig struct { Id
我有以下函数应该从我的想法表中检索一行: func (s *IdeaService) GetIdea(id int64) (*ideaservice.Idea, error) { stmt,
请耐心等待,我的 MAPI 知识很少,我的 C++ 技能也很初级...... 我有一个程序,可以使用 MAPI 从 Exchange 服务器读取所有可能的日历约会。除了一种奇怪的情况外,一切都进展顺利
我正在尝试实现一个简单的 POST API 以在 Postgres 数据库中插入数据,如下所示: http://123.123.10.10/checkin?userid=clive&token=123
在 YII 中,使用 queryRow 函数获取单行 $SQL = //SQL QUERY $data = Yii::app()->db->createCommand($SQL); $result =
我想检索该行的所有字段,然后将它们呈现为 html。我知道该怎么做,这里是一个包含 3 个字段的行的代码: type View struct { Id int Name
我试着用beego来进行木板寻呼。 所以我是这样做的, 在控制器中 var articles []*models.Board board.Data["startCount"] = startCount
我想从 sql 数据库中获取一行。我使用以下代码: var name string row := db.Con().QueryRow("SELECT name FROM users WHERE id
我们正在使用一个包含很多字段的用户结构,如下所示: type user struct { ID int `json:"id,omitempty"` UUID
我花了很多时间试图解决这个问题。 我有一个结构: type Token struct { Id *int64 `db:"id"` Email
我想执行这样的查询(使用 MySql): select * from user where id = 5 Ex 1. 这将返回 err = sql.ErrNoRows: err := db.Query
两者之间是否存在显着的性能差异 func (db *DB) Query(query string, args ...interface{}) (*Rows, error) 和 func (db *DB
Go 数据库/sql Postgres 适配器不支持 LastInsertId。 From the docs : pq does not support the LastInsertId() meth
我是一名优秀的程序员,十分优秀!