1

I'm having trouble to create a endpoint on a Beego application

So, I just put some object information on the returned JSON:

// GetOne ...
// @Title GetOne
// @Description get Migration by id
// @Param   id      path    string  true        "The key for staticblock"
// @Success 200 {object} models.Migration
// @Failure 403 :id is empty
// @router /:id [get]
func (c *MigrationController) GetOne() {
    val, err := mg.Data["json"] = map[string]string{
        "MigrationId": c.MigrationId
        "Status": c.Status
        "Created": c.Created
        "Updated": c.Updated
    }

    if err != nil {
        log.Debug("Fail - GetOne: %v", err)
    } else {
        mg.ServeJSON()
    }

When I tried to call the endpoint, I get this

Handler crashed with error can't find templatefile in the path:views/migrationcontroller/getone.tpl

I'm not using these templates anywhere in the whole code...

I'm not familiar with this framework, someone can help me?


update:

This was a legacy code, with too many issues, the team decided to not fix that at the time I was still working on this project Therefore, I'm not able to tell what the fixed code would look like

Guilherme
  • 1,705
  • 1
  • 25
  • 45
  • `val, err := mg.Data["json"] = map[string]string{ ...` is not valid Go, please show code that actually can reproduce the error. – mkopriva Dec 24 '19 at 13:53
  • This is the code. Is a api, when i try to access, i get that error – Guilherme Dec 24 '19 at 13:55
  • If this is the code then start by fixing the invalid Go code, then, after you have something that can actually compile, you can start debugging whatever other issues you may have. – mkopriva Dec 24 '19 at 14:01
  • If you are using some "tool" that fixes your invalid code for you before compiling and running the app, then please include that in your question, and also include the command you're using to run that tool. – mkopriva Dec 24 '19 at 14:03
  • I am not familiar with golang. I am trying to make this change on a legacy system. Could you suggest me a way to change this? – Guilherme Dec 24 '19 at 14:10
  • The code is broken in such a way that it is hard for me to understand what you're *trying* to do and therefore I am unable to provide suggestions to what you *should* do. All I can say is that what you have there is not valid Go code and it will not compile using the standard `go build` command. Try runnig [this code example](https://play.golang.com/p/z5yRjmu87Bp) and you'll see that it won't compile. Not only is `val, err := mg.Data["json"] = map[string]string{ ...` invalid syntax, but you're also missing commas, which are required in Go, in the map's composite literal. – mkopriva Dec 24 '19 at 14:21

1 Answers1

2

You should use ServeJSON() with current controller.

func (c *MigrationController) GetOne() {
     defer c.ServeJSON()
     ...
}
ahmetlutfu
  • 325
  • 2
  • 9