Coder Social home page Coder Social logo

Comments (14)

eddycjy avatar eddycjy commented on May 16, 2024 2

没事,你有学到东西就好。欢迎随时有问题再反馈 👍

from go-gin-example.

eddycjy avatar eddycjy commented on May 16, 2024

@sun-wenming 你还需要更多的信息给我,这样子我才能更好的帮助你 Debug。希望你能够提供如下信息:

  • 请求的是哪个接口(调用的哪个方法)?
  • 接口的入参是怎么样的?
  • 数据库的表初始化 SQL

from go-gin-example.

eddycjy avatar eddycjy commented on May 16, 2024

简单来说,就是我希望能够模拟与您一模一样的情况

from go-gin-example.

sunwenming avatar sunwenming commented on May 16, 2024
  1. 请求的接口为 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}"
  1. 入参是通过 path 与 body
    如: http://localhost:8000/api/v1/topics/1
    body字段为 topic : 数据为:(可以根据 上述curl 或 swagger网址调用)
{
  "modified_by": 1,
  "name": "主题名称",
  "state": 1
}
  1. 数据库初始化sql文件 luoliluosuo.sql 数据库名称为 luoliluosuo
    在项目的conf配置文件下。数据库 用户 密码默认为 root

PS:我是根据 煎鱼大大的项目改造的练手demo。写了几天之后手抖多点了一次请求发现了这个问题。望可以帮助解决下。想知其所以然。谢谢。

from go-gin-example.

liangyu422 avatar liangyu422 commented on May 16, 2024

反向否定预查格式是(?<!pattern) 反向不匹配。(?<!J)a,不匹配紧跟字母J后面的a
楼上两位大佬,知道go是怎么样解决的么? go的正则默认不支持反向预查。

from go-gin-example.

sunwenming avatar sunwenming commented on May 16, 2024

@liangyu422 我还未接触到此类型的知识,刚入门。 抱歉。

from go-gin-example.

eddycjy avatar eddycjy commented on May 16, 2024

@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.

eddycjy avatar eddycjy commented on May 16, 2024

我看到你已经把 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.

sunwenming avatar sunwenming commented on May 16, 2024

谢谢您的指点。
之前专门写个方法查询数据库,判断此ID的数据是否存在,感觉多执行了一条sql语句,影响速度(菜鸟臆想)。
因此写了如下操作。
image
导致习惯性的用一个变量接收返回的信息。没有意识到这是全局变量。犯了很低级错误。
感谢指导。

from go-gin-example.

eddycjy avatar eddycjy commented on May 16, 2024

判断数据是否存在的话,看实际需求,合理的 SQL 都是没有问题的

from go-gin-example.

liangyu422 avatar liangyu422 commented on May 16, 2024

没事,你有学到东西就好。欢迎随时有问题再反馈 👍

好热心的人

from go-gin-example.

sunwenming avatar sunwenming commented on May 16, 2024

嗯嗯,谢谢。
还有很长的路要走~
努力踩坑~

from go-gin-example.

sunwenming avatar sunwenming commented on May 16, 2024

@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.

eddycjy avatar eddycjy commented on May 16, 2024

@sun-wenming 已修改,感谢反馈 👍

from go-gin-example.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.