2

当我将事件 id 传递给 DisplayScanMembers(23) 时,如果 event_id 没有出现在数据库中,它会返回一个空集。然而,显然一个空集相当于一个零指针(我猜)。因此,我无法在 DisplayScanMembers 函数中将值(零指针)分配给“member_s”。

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

我得到的错误:

错误 2014/10/22 23:08:19 panic.go:29:运行时错误:无效的内存地址或 nil 指针取消引用 /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;

}
4

1 回答 1

1

我认为您在“getScanMembers”函数中的问题。你的返回语句是 revel.Result,如果你有错误,你这样做:

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

但是你应该这样做:

if err != nil {
    return revel.Result{}, err;
}
于 2015-01-23T15:40:01.283 回答