Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

编写 go-admin 应用,第 1 步 #790

Open
wuqiong818 opened this issue Apr 18, 2024 · 2 comments
Open

编写 go-admin 应用,第 1 步 #790

wuqiong818 opened this issue Apr 18, 2024 · 2 comments

Comments

@wuqiong818
Copy link

文档连接:https://mydearzwj.gitee.io/go-admin-doc/guide/intro/tutorial01.html#%E5%BC%80%E5%A7%8B%E9%A1%B9%E7%9B%AE

测试代码和错误结果:
app/admin/apis/article.go
package apis

import (
"fmt"
"github.com/gin-gonic/gin"
"go-admin/common/apis"
)

type Article struct {
apis.Api
}

func (a *Article) GetArticleList(c *gin.Context) {
fmt.Println("a是空的吗?", a)
fmt.Println("请求成功,准备数据的返回")
a.OK("hello world!", "success")
//可以改成以下代码,直接进行返回,不要使用a.OK方法,不然会出现空指针异常
//response.OK(c, "hello world!", "success")
}
app/admin/router/article.go
package router

import (
"fmt"
"github.com/gin-gonic/gin"
jwt "github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth"
"go-admin/app/admin/apis"
)

func init() {
routerCheckRole = append(routerCheckRole, registerArticleRouter)
}

// 需认证的路由代码
func registerArticleRouter(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) {
api := &apis.Article{}
r := v1.Group("")
{
fmt.Println("是不是空的api", api)
r.GET("/articleList", api.GetArticleList)
}
}
** 错误返回
{
"code": 500,
"msg": "runtime error: invalid memory address or nil pointer dereference"
}

建议更改方式,见app/admin/apis/article.go的注释,要么使用原生的response.OK()返回;要么给方法的revecier接收者进行赋初值操作。

@wuqiong818
Copy link
Author

func (a *Article) GetArticleList(c *gin.Context) {
err := a.MakeContext(c).
MakeOrm().
Errors
if err != nil {
a.Logger.Error(err)
a.Error(500, err, err.Error())
return
}
fmt.Println("a是空的吗?", a)
fmt.Println("请求成功,准备数据的返回")
a.OK("hello world!", "success")
}

@wenjianzhang
Copy link
Member

文档中有部分内容没有跟上版本的更新,对此我们深表歉意,会尽量检查更新,同时也希望您可以提交文档的pr,谢谢!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants