How to get fiber.Ctx inside Gorm's AfterCreate() hook

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

How to get fiber.Ctx inside Gorm's AfterCreate() hook

问题

在 Gorm 的 AfterCreate() 钩子中,我需要调用一些 Fiber 上下文的方法,比如 MultipartForm()SaveFile(),该如何做到?

英文:

I need to call some methods of fiber context like MultipartForm() and SaveFile() inside Gorm's AfterCreate() hook, how to do that?

答案1

得分: 2

你可以在结构体中添加一个字段来保存fiber.Ctx,然后在AfterCreate钩子函数中调用该字段的方法。

type User struct {
  c *fiber.Ctx
  // 其他字段
}

func (user *User) AfterCreate(tx *gorm.DB) (err error) {
	// user.c.MultipartForm()/ SaveFile()
	return nil
}
英文:

You can add a field in the struct to hold fiber.Ctx in. Then call methods on this in AfterCreate hook.

type User struct {
  c *fiber.Ctx
  <Other fields>
}

func (user *User) AfterCreate(tx *gorm.DB) (err error) {
	// user.c.MultipartForm()/ SaveFile()
	return nil
}

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

发表评论

匿名网友

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

确定