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 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题