如何將數據從控制器傳遞到 go lang 中的表單? (How can I pass data from controller to form in go lang?)


問題描述

如何將數據從控制器傳遞到 go lang 中的表單? (How can I pass data from controller to form in go lang?)

我有一個接收 http 請求的處理程序/控制器。

func UpdateHandler(request *http.Request) {
    ID := mux.Vars(request)["ID"]
    UpdateForm.Save(ID,db)
}

然後我有一個表單,我想處理數據並最終更新它。

type UpdateForm struct {
    ID              string            `json:"type"`
}

func (UpdateForm) Save(db mongo.Database) {
    id := ID
    repository.Update(Id)
}

開始將打印出 undefined ID

如何確保表單從控制器獲取值?


參考解法

方法 1:

You can populate your form using data from the request. If your request contains a JSON encoded body than you could decode it into your form object like this:

package main

import (
    "encoding/json"
    "net/http"
    "strings"
    "fmt"
)

type UpdateForm struct {
    ID string `json:"type"`
}

func main() {
    req, _ := http.NewRequest(
        "POST",
        "http://example.com",
        strings.NewReader(`{"type": "foo"}`),
    )

    var form *UpdateForm
    json.NewDecoder(req.Body).Decode(&form)
    fmt.Println(form.ID) // Output: foo
}

Or you can instantiate it directly like this:

func UpdateHandler(request *http.Request) {
    ID := mux.Vars(request)["ID"]
    form := &UpdateForm{ID: ID}
    form.Save()
}

方法 2:

I think it has nothing to do with the handler, but your code isn't consistent. This line

UpdateForm.Save(ID,db)

The method Save() takes two arguments, while the original method signature takes only a single mongo.Database type argument.

Here is what I assume was your intention:

type UpdateForm struct {
    ID     string   `json:"type"`
}

func (u UpdateForm) Save(db mongo.Database) {
    id := u.ID
    repository.Update(id)
}

// UpdateForm instance somewhere
var u = UpdateForm{}

func UpdateHandler(request *http.Request) {
    u.ID := mux.Vars(request)["ID"]
    u.Save(db)
}

(by thatgibbyguyviallyPandemonium)

參考文件

  1. How can I pass data from controller to form in go lang? (CC BY‑SA 2.5/3.0/4.0)

#API #GO






相關問題

UPS Api - php, làm cách nào để truy cập? (UPS Api - php, how do I get around?)

Google api 地圖和主幹錯誤 (Google api maps and backbone error)

如何將數據從控制器傳遞到 go lang 中的表單? (How can I pass data from controller to form in go lang?)

從 ASP.net 使用 PHP 中的 POST 數據抓取數據 (Scraping data with POST data in PHP from ASP.net)

檢測/指紋手機品牌和型號的替代方法? (Alternative way to detect/fingerprint phone make and model?)

POST:get_responses API 未提供所需數據 (POST : get_responses API not giving required data)

通過 API 檢測 Shopware 版本 (Detect Shopware Version through API)

我的 module.exports 返回為 [object Object] (My module.exports return as [object Object])

API 憑證應該存儲在 laravel 中的哪個位置? (Where API credentials should be stored in laravel?)

Uppy "PUT" XHR(multipart/form-data) 請求結果清空 Laravel $request 數組 (Uppy "PUT" XHR(multipart/form-data) request results to empty Laravel $request array)

JObject 不包含“user_name”的定義,也沒有可訪問的擴展方法“user_name” (JObject does not contain a definition for 'user_name' and no accessible extension method 'user_name')

PUT API 中的 Django Rest Framework 解析錯誤 (Django Rest Framework parse error in PUT API)







留言討論