undefinedfix
Sign in

What's wrong with this golang code? Please have a look

NWSDRArvada edited in Sun, 22 May 2022

What's wrong with this golang code? Please help me to have a look: This is a user registration function, the code is as follows:


func Signup(c *gin.Context) {
    var r CreateRequest
    if err := c.Bind(&r); err != nil {
        SendResponse(c, errno.ErrBind, nil)
        return
    }
    u := model.User{
        Username: r.Username,
        Phone:    r.Phone,
        Password: r.Password,
    }

    // 参数校验
    if err := u.Validate(); err != nil {
        SendResponse(c, errno.ErrValidation, nil)
        fmt.Println(err) //打印错误
        return
    }
    // 加密密码
    if err := u.Encrypt(); err != nil {
        SendResponse(c, errno.ErrEncrypt, nil)
        return
    }
    // 在数据库中添加数据记录
    if err := u.Create(); err != nil {
        SendResponse(c, errno.ErrDatabase, nil)
        fmt.Println(err) //打印错误
        return
    }
    // Sign the json web token.
    t, err := token.Sign(c, token.Context{ID: u.Id, Username: u.Username}, "")
    if err != nil {
        SendResponse(c, errno.ErrToken, nil)
        return
    }
    SendResponse(c, nil, model.Token{Token: t})
}

Problem: successfully insert a piece of data into the database, but the username and phone are empty, as shown in the following screenshot:

clipboard.png

clipboard.png

What is the reason?

2 Replies
kiajt
commented on Sun, 22 May 2022

What about the implementation of your create function?

jakab922
commented on Sun, 22 May 2022

You see if you got the user name and phone

lock This question has been locked and the reply function has been disabled.