数据库返回空集时,内存地址无效或nil指针取消引用

时间:2014-10-23 04:49:32

标签: go revel gorp

当我将事件id传递给DisplayScanMembers(23)时,如果event_id没有出现在数据库中,则返回一个空集。但是,显然一个空集相当于一个零指针(我猜)。因此,我无法在DisplayScanMembers函数中将值(nil指针)赋给'member_s'。

如果数据库查询返回一个空集,我需要的是getScanMembers方法返回一个空数组而不是nil。

我得到的错误:

错误2014/10/22 23:08:19 panic.go:29:运行时错误:无效的内存地址或无指针取消引用 /home/developer/gocode/src/drakecheckin/app/controllers/scan.go:236(0x4a21f8)

func (c Scan) DisplayScanMembers(id int64) revel.Result{

if (id == -1 || id == 0){ 
    event,_ :=c.getLatestSelectedEvent();
    member_s,_ := c.getScanMembers(event.EventId);
    return members;
} else { 
    member_s,err2 := c.getScanMembers(id);
    if err2 !=nil {
                event,_ :=c.getLatestSelectedEvent();
                member_s,_ = c.getScanMembers(event.EventId);
    }
    return member_s;
}

}


func (c Scan) getScanMembers(event_id int64) (revel.Result,error) {

group_id,_ := convStrInt(c.Session["group_id"]);
org_id,_ := convStrInt(c.Session["org_id"]);;
query := "SELECT me.member_event_id,m.member_first_name,m.member_last_name,m.member_uniq_num "+
          "FROM MemberEvent AS me JOIN Member AS m ON m.member_id =me.member_id AND m.org_id =me.org_id "+
          "WHERE me.group_id=? AND me.org_id=? AND me.event_id=?"; 

members, err := c.Txn.Select(models.AllMemberEvent{}, query, group_id, org_id, event_id);
if err != nil {
    return nil,err;
}

return c.RenderJson(members),nil;

}

1 个答案:

答案 0 :(得分:1)

我认为你的问题在" getScanMembers"功能。你的return语句是revel.Result,如果你有错误,你这样做:

if err != nil {
    return nil, err;
}

但你应该这样做:

if err != nil {
    return revel.Result{}, err;
}