Error:interface must be a pointer to struct Error Returned in fiber (golang), how to solve this?

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

Error:interface must be a pointer to struct Error Returned in fiber (golang), how to solve this?

问题

我是你的中文翻译助手,以下是你提供的代码的翻译:

我是Golang编程的新手
我遇到了一个问题..
我试图通过"BodyParser"函数访问我的发送的请求体数据
但是我遇到了一个错误

schema: 接口必须是指向结构体的指针

以下是我提供的函数:

func CreateService(c *fiber.Ctx) error {
    if c.Locals("user_type") != "1" {
        return c.SendString("Wrong One")
    }

    file, err := c.FormFile("image")

    // 检查错误:
    if err != nil {
        fmt.Println(err.Error())
        return c.JSON("Something error")
    }
    // 保存文件到根目录:
    c.SaveFile(file, fmt.Sprintf("./%s", file.Filename))
    // 保存文件到当前工作目录下的uploads文件夹中:
    c.SaveFile(file, fmt.Sprintf("./uploads/%s", file.Filename))
    // 使用相对路径保存文件:
    c.SaveFile(file, fmt.Sprintf("/tmp/uploads_relative/%s", file.Filename))

    var data map[string]string

    if err := c.BodyParser(&data); err != nil {
        return err
    }

    service := models.Services{
        Title: data["title"],
        Src:   PORT + "/" + file.Filename,
    }
    database.DB.Create(&service)

    return c.JSON(service)
}

models.Services的定义如下:

type Services struct {
    Id    uint   `json:"id"`
    Title string `json:"title"`
    Src   string `json:"src"`
}

请帮我解决问题。非常感谢!

英文:

I'm new in Golang Programming
I'm Faceing an issue..
I'm trying to acces my sent body data by "BodyParser"functtion
But I got an error

> schema: interface must be a pointer to struct

I'm Giving the Function Bellow

func CreateService(c *fiber.Ctx) error {
	if c.Locals("user_type") != "1" {
		return c.SendString("Wrong One")

	}

	file, err := c.FormFile("image")

	// Check for errors:
	if err != nil {
		fmt.Println(err.Error())
		return c.JSON("Something error")
	}
	// 👷 Save file to root directory:
	c.SaveFile(file, fmt.Sprintf("./%s", file.Filename))
	// 👷 Save file inside uploads folder under current working directory:
	c.SaveFile(file, fmt.Sprintf("./uploads/%s", file.Filename))
	// 👷 Save file using a relative path:
	c.SaveFile(file, fmt.Sprintf("/tmp/uploads_relative/%s", file.Filename))

	var data map[string]string

	if err := c.BodyParser(&data); err != nil {
		return err
	}

	service := models.Services{
		Title: data["title"],
		Src:   PORT + "/" + file.Filename,
	}
	database.DB.Create(&service)

	return c.JSON(service)
}

model.Services is

type Services struct {
	Id    uint   `json:"id"`
	Title string `json:"title"`
	Src   string `json:"src"`
}

Please Help me out. Thanks a lot in advance!!!

答案1

得分: 1

我们需要在GoFiber_v2中提供一个指向结构体的指针,而不是*map[string]string。这是导致问题的原因。

以下是从GoFiber_v2 BodyParser获取数据的函数:

func Register(c *fiber.Ctx) error {
    var user models.User

    err := c.BodyParser(&user)
    if err != nil {
        return err
    }

    fmt.Printf("%#+v", user)
}

模型包:

package models

type User struct {
    Id        uint   `json:"id"`
    FirstName string `json:"first_name"`
    LastName  string `json:"last_name"`
    Email     string `json:"email"`
    Password  string `json:"password"`
}
英文:

We need to provide pointer to a struct in GoFiber_v2 not *map[string]string. This is causing the issue.

Below Function for getting data from GoFiber_v2 BodyParser:

func Register(c *fiber.Ctx) error {
    var user models.User

    err := c.BodyParser(&user)
    if err != nil {
	return err
    }

    fmt.Printf("%#+v", user)
}

Model Package

package models

type User struct {
    Id        uint   `json:"id"`
    FirstName string `json:"first_name"`
    LastName  string `json:"last_name"`
    Email     string `json:"email"`
    Password  string `json:"password"`
}

huangapple
  • 本文由 发表于 2021年12月24日 05:27:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/70467545.html
匿名

发表评论

匿名网友

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

确定