duanli9930 2018-11-14 03:53
浏览 209
已采纳

使用beego验证码:无效的内存地址或nil指针取消引用

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}}&zwnj;{{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

  • go version go1.10 windows/amd64
  • Beego : 1.10.1
  • 写回答

1条回答 默认 最新

  • duanqian3953 2018-11-14 04:22
    关注

    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 _.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。