invalid operation: cannot call non-function mysql.MysqlDB.Save(product).Error (variable of type error)

huangapple go评论109阅读模式
英文:

invalid operation: cannot call non-function mysql.MysqlDB.Save(product).Error (variable of type error)

问题

谢谢,我解决了这个问题。

Gorm调用Error()时出现了问题,所以我将其修改为以下代码,然后问题就解决了:

package service

import (
   "iris-seckill/db/mysql"
   "iris-seckill/model"
)

type IProductService interface {
   GetProductByID(int64) (*model.Product, error)
   GetAllProduct() ([]*model.Product, error)
   DeleteProductByID(int64) error
   InsertProduct(product *model.Product) (int64, error)
   UpdateProduct(product *model.Product) error
}

type ProductService struct {
}

func NewProductService() IProductService {
   return &ProductService{}
}

func (p *ProductService) GetProductByID(productID int64) (*model.Product, error) {
   var product model.Product
   mysql.MysqlDB.Where("product_id = ?", productID).First(&product)
   return &product, nil
}

func (p *ProductService) GetAllProduct() ([]*model.Product, error) {
   var products []*model.Product
   mysql.MysqlDB.Find(&products)
   return products, nil
}

func (p *ProductService) DeleteProductByID(productID int64) error {
   mysql.MysqlDB.Where("product_id = ?", productID).Delete(&model.Product{})
   return nil
}

func (p *ProductService) InsertProduct(product *model.Product) (int64, error) {
   mysql.MysqlDB.Create(product)
   return product.ProductID, nil
}

func (p *ProductService) UpdateProduct(product *model.Product) error {
   mysql.MysqlDB.Save(product)
   return nil
}

但是不清楚为什么会出现这个问题。

我在在线课堂中模仿老师的代码,但在使用GORM时遇到了一个非常困难的错误。

错误信息为:invalid operation: cannot call non-function mysql.MysqlDB.Save(product).Error (variable of type error)

控制器层的代码如下:

package controller

import (
   "iris-seckill/model"
   "iris-seckill/service"

   "github.com/kataras/iris/v12"
   "github.com/kataras/iris/v12/mvc"
)

type ProductController struct {
   Ctx            iris.Context
   ProductService service.IProductService
}

func (p *ProductController) GetAll() (mvc.View, error) {
   products, err := p.ProductService.GetAllProduct()
   if err != nil {
      return mvc.View{}, err
   }
   return mvc.View{
      Name: "product/view.html",
      Data: iris.Map{
         "products": products,
      },
   }, nil
}

func (p *ProductController) PostUpdate() {
   product := &model.Product{}
   p.Ctx.Request().ParseForm()
   err := p.ProductService.UpdateProduct(product)
   if err != nil {
      p.Ctx.Application().Logger().Debug(err)
   }
   p.Ctx.Redirect("/product/all")
}

服务层的代码如下:

package service

import (
   "iris-seckill/db/mysql"
   "iris-seckill/model"
)

type IProductService interface {
   GetProductByID(int64) (*model.Product, error)
   GetAllProduct() ([]*model.Product, error)
   DeleteProductByID(int64) error
   InsertProduct(product *model.Product) (int64, error)
   UpdateProduct(product *model.Product) error
}

type ProductService struct {
}

func NewProductService() IProductService {
   return &ProductService{}
}

func (p *ProductService) GetProductByID(productID int64) (*model.Product, error) {
   var product model.Product
   err := mysql.MysqlDB.Where("product_id = ?", productID).First(&product).Error()
   if err != nil {
      return nil, err
   }
   return &product, nil
}

func (p *ProductService) GetAllProduct() ([]*model.Product, error) {
   var products []*model.Product
   err := mysql.MysqlDB.Find(&products).Error()
   return products, err
}

func (p *ProductService) DeleteProductByID(productID int64) error {
   err := mysql.MysqlDB.Where("product_id = ?", productID).Delete(&model.Product{}).Error()
   return err
}

func (p *ProductService) InsertProduct(product *model.Product) (int64, error) {
   err := mysql.MysqlDB.Create(product).Error()
   if err != nil {
      return 0, err
   }
   return product.ProductID, nil
}

func (p *ProductService) UpdateProduct(product *model.Product) error {
   err := mysql.MysqlDB.Save(product).Error()
   return err
}

main.go的代码如下:

package main

import (
   "context"
   "iris-seckill/backend/web/controller"
   "iris-seckill/conf"
   "iris-seckill/db/mysql"
   "iris-seckill/service"

   "github.com/kataras/iris/v12"
   "github.com/kataras/iris/v12/mvc"
   logging "github.com/sirupsen/logrus"
)

func main() {
   app := iris.New()
   app.Logger().SetLevel("debug")

   tmplate := iris.HTML("./backend/web/assets", ".html").Layout("share/layout").Reload(true)
   app.RegisterView(tmplate)

   app.HandleDir("/assets", "./backend/web/assets")

   app.OnAnyErrorCode(func(ctx iris.Context) {
      ctx.ViewData("message", ctx.Values().GetStringDefault("message", "访问的页面出错!"))
      ctx.ViewLayout("")
      ctx.View("share/error.html")
   })


   ctx, cancel := context.WithCancel(context.Background())
   defer cancel()
   productService := service.NewProductService()
   productParty := app.Party("/product")
   productApp := mvc.New(productParty)
   productApp.Register(ctx, productService)
   productApp.Handle(new(controller.ProductController))

   err := app.Run(
      iris.Addr(conf.IrisAddr),
      iris.WithoutServerError(iris.ErrServerClosed),
      iris.WithOptimizations,
   )
   if err != nil {
      logging.Info(err)
   }
}

func init() {
   conf.Init("./conf/config.ini")
   mysql.Init()
}

项目的目录结构如下:https://i.stack.imgur.com/hNGIH.png

错误信息如下:https://i.stack.imgur.com/sgIEc.png

MysqlDB 已经工作并通过了测试。

我真的不知道出了什么问题,即使调试也报错。

希望有人能帮助我,非常感谢您能给予的任何帮助。谢谢!

另外,我和视频中的老师唯一的区别是老师编写了自己的数据库层操作,而我使用了Gorm,不知道这是否是问题所在。

英文:

> Thanks, man. I solved the problem.
>
> There was a problem with Gorm calling Error(), so I wrote it like this and it worked fine:
>
> go
> package service
>
> import (
> "iris-seckill/db/mysql"
> "iris-seckill/model"
> )
>
> type IProductService interface {
> GetProductByID(int64) (*model.Product, error)
> GetAllProduct() ([]*model.Product, error)
> DeleteProductByID(int64) error
> InsertProduct(product *model.Product) (int64, error)
> UpdateProduct(product *model.Product) error
> }
>
> type ProductService struct {
> }
>
> func NewProductService() IProductService {
> return &ProductService{}
> }
>
> func (p *ProductService) GetProductByID(productID int64) (*model.Product, error) {
> var product model.Product
> mysql.MysqlDB.Where("product_id = ?", productID).First(&product)
> return &product, nil
> }
>
> func (p *ProductService) GetAllProduct() (res []*model.Product, err error) {
> mysql.MysqlDB.Find(&res)
> return
> }
>
> func (p *ProductService) DeleteProductByID(productID int64) error {
> mysql.MysqlDB.Where("product_id = ?", productID).Delete(&model.Product{})
> return nil
> }
>
> func (p *ProductService) InsertProduct(product *model.Product) (int64, error) {
> mysql.MysqlDB.Create(product)
> return product.ProductID, nil
> }
>
> func (p *ProductService) UpdateProduct(product *model.Product) error {
> mysql.MysqlDB.Save(product)
> return nil
> }
>

>
> But it's not clear why.

I'm imitating the teacher in the online class,however, I encountered a very difficult error when using GORM.

Error: invalid operation: cannot call non-function mysql.MysqlDB.Save(product).Error (variable of type error)

The code of the controller layer is as follows:

package controller

import (
   "iris-seckill/model"
   "iris-seckill/service"

   "github.com/kataras/iris/v12"
   "github.com/kataras/iris/v12/mvc"
)

type ProductController struct {
   Ctx            iris.Context
   ProductService service.IProductService
}

func (p *ProductController) GetAll() (mvc.View, error) {
   products, err := p.ProductService.GetAllProduct()
   if err != nil {
      return mvc.View{}, err
   }
   return mvc.View{
      Name: "product/view.html",
      Data: iris.Map{
         "products": products,
      },
   }, nil
}

func (p *ProductController) PostUpdate() {
   product := &model.Product{}
   p.Ctx.Request().ParseForm()
   err := p.ProductService.UpdateProduct(product)
   if err != nil {
      p.Ctx.Application().Logger().Debug(err)
   }
   p.Ctx.Redirect("/product/all")
}

The code of the service layer is as follows:

package service

import (
   "iris-seckill/db/mysql"
   "iris-seckill/model"
)

type IProductService interface {
   GetProductByID(int64) (*model.Product, error)
   GetAllProduct() ([]*model.Product, error)
   DeleteProductByID(int64) error
   InsertProduct(product *model.Product) (int64, error)
   UpdateProduct(product *model.Product) error
}

type ProductService struct {
}

func NewProductService() IProductService {
   return &ProductService{}
}

func (p *ProductService) GetProductByID(productID int64) (*model.Product, error) {
   var product model.Product
   err := mysql.MysqlDB.Where("product_id = ?", productID).First(&product).Error()
   if err != nil {
      return nil, err
   }
   return &product, nil
}

func (p *ProductService) GetAllProduct() (res []*model.Product, err error) {
   err = mysql.MysqlDB.Find(&res).Error()
   return
}

func (p *ProductService) DeleteProductByID(productID int64) error {
   err := mysql.MysqlDB.Where("product_id = ?", productID).Delete(&model.Product{}).Error()
   return err
}

func (p *ProductService) InsertProduct(product *model.Product) (int64, error) {
   err := mysql.MysqlDB.Create(product).Error()
   if err != nil {
      return 0, err
   }
   return product.ProductID, nil
}

func (p *ProductService) UpdateProduct(product *model.Product) error {
   err := mysql.MysqlDB.Save(product).Error()
   return err
}

The main.go code is as follows:

package main

import (
   "context"
   "iris-seckill/backend/web/controller"
   "iris-seckill/conf"
   "iris-seckill/db/mysql"
   "iris-seckill/service"

   "github.com/kataras/iris/v12"
   "github.com/kataras/iris/v12/mvc"
   logging "github.com/sirupsen/logrus"
)

func main() {
   app := iris.New()
   app.Logger().SetLevel("debug")

   tmplate := iris.HTML("./backend/web/assets", ".html").Layout("share/layout").Reload(true)
   app.RegisterView(tmplate)

   app.HandleDir("/assets", "./backend/web/assets")

   app.OnAnyErrorCode(func(ctx iris.Context) {
      ctx.ViewData("message", ctx.Values().GetStringDefault("message", "访问的页面出错"))
      ctx.ViewLayout("")
      ctx.View("share/error.html")
   })


   ctx, cancel := context.WithCancel(context.Background())
   defer cancel()
   productService := service.NewProductService()
   productParty := app.Party("/product")
   productApp := mvc.New(productParty)
   productApp.Register(ctx, productService)
   productApp.Handle(new(controller.ProductController))

   err := app.Run(
      iris.Addr(conf.IrisAddr),
      iris.WithoutServerError(iris.ErrServerClosed),
      iris.WithOptimizations,
   )
   if err != nil {
      logging.Info(err)
   }
}

func init() {
   conf.Init("./conf/config.ini")
   mysql.Init()
}

The project directory structure is as follows: https://i.stack.imgur.com/hNGIH.png

The error message is as follows: https://i.stack.imgur.com/sgIEc.png

MysqlDB worked and passed the test.

I really don't know what's wrong, even debug is reporting an error.

I hope someone can help me, I appreciate any help you can give me.Thank you!

> By the way, the only difference between me and the video teacher is that the teacher wrote his own database layer operations and I used Gorm,I wonder if that's the point.

答案1

得分: 0

在Gorm中,注意.Error.Error()之间的区别。

为了解决这个问题,编辑服务层代码如下

package service

import (
   "iris-seckill/db/mysql"
   "iris-seckill/model"
)

type IProductService interface {
   GetProductByID(int64) (*model.Product, error)
   GetAllProduct() ([]*model.Product, error)
   DeleteProductByID(int64) error
   InsertProduct(product *model.Product) (uint, error)
   UpdateProduct(product *model.Product) error
}

type ProductService struct {
}

func NewProductService() IProductService {
   return &ProductService{}
}

func (p *ProductService) GetProductByID(id int64) (*model.Product, error) {
   var product model.Product
   err := mysql.MysqlDB.Where("id = ?", id).First(&product).Error
   return &product, err
}

func (p *ProductService) GetAllProduct() (res []*model.Product, err error) {
   err = mysql.MysqlDB.Find(&res).Error
   return
}

func (p *ProductService) DeleteProductByID(id int64) error {
   err := mysql.MysqlDB.Where("id = ?", id).Delete(&model.Product{}).Error
   return err
}

func (p *ProductService) InsertProduct(product *model.Product) (uint, error) {
   err := mysql.MysqlDB.Create(product).Error
   return product.ID, err
}

func (p *ProductService) UpdateProduct(product *model.Product) error {
   err := mysql.MysqlDB.Save(product).Error
   return err
}

这真是我的疏忽。

英文:

In Gorm, notice the difference between .Error and .Error()

To solve this problem,edit the code of the service layer is as follows:

package service

import (
   "iris-seckill/db/mysql"
   "iris-seckill/model"
)

type IProductService interface {
   GetProductByID(int64) (*model.Product, error)
   GetAllProduct() ([]*model.Product, error)
   DeleteProductByID(int64) error
   InsertProduct(product *model.Product) (uint, error)
   UpdateProduct(product *model.Product) error
}

type ProductService struct {
}

func NewProductService() IProductService {
   return &ProductService{}
}

func (p *ProductService) GetProductByID(id int64) (*model.Product, error) {
   var product model.Product
   err := mysql.MysqlDB.Where("id = ?", id).First(&product).Error
   return &product, err
}

func (p *ProductService) GetAllProduct() (res []*model.Product, err error) {
   err = mysql.MysqlDB.Find(&res).Error
   return
}

func (p *ProductService) DeleteProductByID(id int64) error {
   err := mysql.MysqlDB.Where("id = ?", id).Delete(&model.Product{}).Error
   return err
}

func (p *ProductService) InsertProduct(product *model.Product) (uint, error) {
   err := mysql.MysqlDB.Create(product).Error
   return product.ID, err
}

func (p *ProductService) UpdateProduct(product *model.Product) error {
   err := mysql.MysqlDB.Save(product).Error
   return err
}

It was really my carelessness.

huangapple
  • 本文由 发表于 2022年7月29日 16:04:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/73163487.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定