Comments (14)
没事,你有学到东西就好。欢迎随时有问题再反馈 👍
from go-gin-example.
@sun-wenming 你还需要更多的信息给我,这样子我才能更好的帮助你 Debug。希望你能够提供如下信息:
- 请求的是哪个接口(调用的哪个方法)?
- 接口的入参是怎么样的?
- 数据库的表初始化 SQL
from go-gin-example.
简单来说,就是我希望能够模拟与您一模一样的情况
from go-gin-example.
- 请求的接口为
http://localhost:8000/api/v1/topics/{id}
调用的方法为
gin-blog\routers\api\v1\topic.go
EditTopic 方法
验证完成跳转 service\topic_service\topic.go
下的Edit 数据处理
之后 执行 models 下的 topic.go的
EditTopic方法执行最后的数据库操作
如果可以,直接用curl 可以访问,或者根据 项目README.md
curl -X PUT "http://localhost:8000/api/v1/topics/1" -H "accept: application/json" -H "Content-Type: application/json" -d "{ \"modified_by\": 1, \"name\": \"主题名称\", \"state\": 1}"
- 入参是通过
path 与 body
如:http://localhost:8000/api/v1/topics/1
body字段为 topic : 数据为:(可以根据 上述curl 或 swagger网址调用)
{
"modified_by": 1,
"name": "主题名称",
"state": 1
}
- 数据库初始化sql文件 luoliluosuo.sql 数据库名称为
luoliluosuo
在项目的conf配置文件下。数据库 用户 密码默认为root
PS:我是根据 煎鱼大大的项目改造的练手demo。写了几天之后手抖多点了一次请求发现了这个问题。望可以帮助解决下。想知其所以然。谢谢。
from go-gin-example.
反向否定预查格式是(?<!pattern) 反向不匹配。(?<!J)a,不匹配紧跟字母J后面的a
楼上两位大佬,知道go是怎么样解决的么? go的正则默认不支持反向预查。
from go-gin-example.
@liangyu422 我还未接触到此类型的知识,刚入门。 抱歉。
from go-gin-example.
@sun-wenming 我认真看了你的代码,你在这块犯了一个低级错误,代码如下:
func EditTopic(id int, data interface{}) error {
fmt.Println(db) //map[name:主题名称 modified_by:1]
db = db.Model(&Topic{}).Where("id = ? ", id).Updates(data)
err := db.Error
logging.Info(err)
if err != nil {
return err
}
return nil
}
你想想有没有什么问题?
from go-gin-example.
我看到你已经把 db
打印出来了,可能就差一步了。详细的分析如下:
【问题】
db 这一个变量,它是一个全局变量。而在这里。你把 db 链式又重新赋予给了 db。因此导致了 db 变量不断地叠加。这就是你这个 BUG 的问题所在。
你可以把代码改成这样,修改后的代码如下:
func EditTopic(id int, data interface{}) error {
if err := db.Model(&Topic{}).Where("id = ? ", id).Updates(data).Error; err != nil {
logging.Info(err)
return err
}
return nil
}
有两个办法,第一是重新赋予变量,第二种就是不重复叠加赋值。两种方案都可以解决你的问题 😄
from go-gin-example.
谢谢您的指点。
之前专门写个方法查询数据库,判断此ID的数据是否存在,感觉多执行了一条sql语句,影响速度(菜鸟臆想)。
因此写了如下操作。
导致习惯性的用一个变量接收返回的信息。没有意识到这是全局变量。犯了很低级错误。
感谢指导。
from go-gin-example.
判断数据是否存在的话,看实际需求,合理的 SQL 都是没有问题的
from go-gin-example.
没事,你有学到东西就好。欢迎随时有问题再反馈 👍
好热心的人
from go-gin-example.
嗯嗯,谢谢。
还有很长的路要走~
努力踩坑~
from go-gin-example.
@eddycjy 您好,我发现了同样的问题在此库中。GetTags() db重新赋值
另外 如果 pageNum>0
时,
导致http://localhost:8000/api/v1/tags?page=1
查找不到前十条数据。
原因:page=1将导致 result 一直为0
因此我做了如下修改:
func GetTags(pageNum int, pageSize int, maps interface{}) ([]Tag, error) {
var (
tags []Tag
err error
)
if pageSize > 0 && pageNum >= 0 {
err = db.Offset(pageNum).Limit(pageSize).Where(maps).Find(&tags).Error
} else {
err = db.Where(maps).Find(&tags).Error
}
if err != nil && err != gorm.ErrRecordNotFound {
return nil, err
}
return tags, nil
}
from go-gin-example.
@sun-wenming 已修改,感谢反馈 👍
from go-gin-example.
Related Issues (20)
- 自定义http返回码的用意 ?
- tags pagination bug
- runtime error: invalid memory address or nil pointer dereference HOT 1
- jwt签名内容包含了用户的密码,签名只可验证不能保密,会泄漏密码,只包含用户id就好了吧 HOT 2
- go get github.com/EDDYCJY/go-gin-example: invalid proxy URL missing scheme: direct;
- runtime error: invalid memory address or nil pointer dereference
- token怎么更新?
- 在swagger里如何使用token HOT 2
- 很多项目过期了api不能用了
- 能否简化,萌新看不太懂
- 这里应该是 !exists HOT 1
- sql: unknown driver "mysql" (forgotten import?) HOT 1
- Bug: auth api
- auth 404 HOT 1
- cors failed HOT 1
- Vulnerability of dependency "golang.org/x/net" HOT 1
- 获取token后怎么调用路由接口,postman测试url怎么填写
- token错误码
- 授权bug
- 用这套模板做公司入门考核的登录认证接口,结果被告知server的目录设计不合理,考核失败。
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from go-gin-example.