I want to use captcha to generate a verification code under Beego. But it has the error invalid memory address or nil pointer dereference.Does anyone know how to solve this problem? Thank you.
Request Method: GET
Request URL: /accounts/forgotpassword
RemoteAddr: 127.0.0.1
Stack
C:/Go/src/runtime/asm_amd64.s:573
C:/Go/src/runtime/panic.go:505
C:/Go/src/text/template/exec.go:137
C:/Go/src/runtime/asm_amd64.s:573
C:/Go/src/runtime/panic.go:505
C:/Go/src/runtime/panic.go:63
C:/Go/src/runtime/signal_windows.go:167
D:/Google Drive/Work/GoWorkspace/src/github.com/astaxie/beego/utils/captcha/captcha.go:186
D:/Google Drive/Work/GoWorkspace/src/github.com/astaxie/beego/utils/captcha/captcha.go:164
D:/Google Drive/Work/GoWorkspace/src/github.com/astaxie/beego/utils/captcha/captcha.go:267
C:/Go/src/runtime/asm_amd64.s:573
C:/Go/src/reflect/value.go:447
C:/Go/src/reflect/value.go:308
C:/Go/src/text/template/exec.go:667
C:/Go/src/text/template/exec.go:535
C:/Go/src/text/template/exec.go:432
C:/Go/src/text/template/exec.go:405
C:/Go/src/text/template/exec.go:231
C:/Go/src/text/template/exec.go:239
C:/Go/src/text/template/exec.go:194
C:/Go/src/text/template/exec.go:177
C:/Go/src/html/template/template.go:137
D:/Google Drive/Work/GoWorkspace/src/github.com/astaxie/beego/template.go:66
D:/Google Drive/Work/GoWorkspace/src/github.com/astaxie/beego/controller.go:283
D:/Google Drive/Work/GoWorkspace/src/github.com/astaxie/beego/controller.go:234
D:/Google Drive/Work/GoWorkspace/src/github.com/astaxie/beego/controller.go:214
D:/Google Drive/Work/GoWorkspace/src/github.com/astaxie/beego/router.go:863
C:/Go/src/net/http/server.go:2694
C:/Go/src/net/http/server.go:1830
C:/Go/src/runtime/asm_amd64.s:2361
My Code: conf\app.conf
# Cache Provider
CacheProvider = redis
CacheConnection = {"conn":"127.0.0.1:6379"}
controllers\main.go
package controllers
import (
"github.com/astaxie/beego"
"github.com/astaxie/beego/cache"
"github.com/astaxie/beego/utils/captcha"
)
var(
cpt *captcha.Captcha
CacheProvider string = beego.AppConfig.String("CacheProvider")
CacheConnection string = beego.AppConfig.String("CacheConnection")
)
func init() {
store, _ := cache.NewCache(CacheProvider, CacheConnection)
cpt = captcha.NewWithFilter("/accounts/captca/", store)
}
views\forgotpasswordcontroller\get.tpl
<div class="w3-container w3-center">
<form method="post" id="mainForm"class="w3-container" style="margin-top:90px">
<div class="w3-card " style=" padding-left: 0px;
padding-right: 0px; margin-top: 30px;">
<div class="w3-container">
<h1>Reset password</h1>
</div><div class="w3-container" style=" padding-bottom: 16px;">
{{create_captcha}}
<input type="text" class="w3-input "name="captcha"style="outline: none;">
<p style="text-align: left;margin-top: 0px;color:red">
{{if .Errors.Captcha}}
{{.Errors.Captcha}}{{else}}‌{{end}}</p>
<input type="submit" value="Request reset password" onclick="login()" class="w3-button w3-indigo w3-block w3-round-large">
</div>
</div>
</form>
</div>
controllers\forgotpassword.go
package controllers
import (
"github.com/astaxie/beego"
)
type ForgotPasswordController struct {
beego.Controller
}
func (c *ForgotPasswordController) Get() {
beego.Debug("In ForgotPasswordController:Get - Start")
c.Layout = "shared/layout.tpl"
}//end ForgotPasswordController:Get()
func (this *ForgotPasswordController) Post() {
beego.Debug("In ForgotPasswordController:Post - Start")
captchaVerification := cpt.VerifyReq(this.Ctx.Request)
if !captchaVerification {
errormap := make(map[string]string)
beego.Debug("In ForgotPasswordController:Post - captchaVerification Got wrong captcha")
errormap["Captcha"] = "Sorry but the characters you endered didn't match. Please try again"
this.Data["Errors"] = errormap
return
}
} //end ForgotPassword() func
Environment
Just tested your code in my local. The error is coming from cache creation part.
store, err := cache.NewCache(CacheProvider, CacheConnection)
if err != nil {
log.Fatal(err.Error())
os.Exit(0)
}
To get the detailed error, check the err
variable returned from cache.NewCache()
. Also it's a best practice to always log any possible error coming from error object, don't ignore it.
Here is the error log:
2018/11/14 11:13:24 cache: unknown adapter name "redis" (forgot to import?)
The error above is occurred because cache package not able to find redis
adapter. It's because you haven't imported the package. So let's try to import it, then your problem will be solved.
import (
"fmt"
"log"
"os"
"github.com/astaxie/beego"
"github.com/astaxie/beego/cache"
"github.com/astaxie/beego/utils/captcha"
_ "github.com/astaxie/beego/cache/redis" // <----- this one
)
Since we don't interact with the cache redis package directly, import it with _
.