使用go和gin-gonic,我想发布一个包含两个标记字段的简单表单,然后将其保存到mongodb。
以下是表格:
void draw(double cx,double cf)
glColor3f(0.34f,0.34f,0.34f);
glPushMatrix();
glTranslated(0.0, cx, cy);
glRotated(-10, 1.0, 0.0, 0.0);
glutSolidTorus(OUTER_DIAL_RADIUS, OUTER_DIAL_RADIUS+30, 100, 200);
glPopMatrix();
end
处理程序是:
<form action="/quotes/{{ .quote.Id.Hex }}" method="POST">
<input type="text" name="author" value="{{ .quote.Author }}">
<textarea name="body" rows="3">{{ .quote.Body }}</textarea>
<input name="tag" value="" >
<input name="tag" value="" >
<button type="submit">Submit</button>
</form>
现在我的问题是func Create(c *gin.Context) {
db := c.MustGet("db").(*mgo.Database)
quote := models.Quote{}
err := c.Bind("e)
if err != nil {
c.Error(err)
return
}
//To debug
fmt.Println("form post values\n")
for t, v := range c.Request.Form["tag"] {
fmt.Println(t, v)
}
//To debug
fmt.Println(quote)
err = db.C(models.CollectionQuote).Insert(quote)
if err != nil {
c.Error(err)
}
c.Redirect(http.StatusMovedPermanently, "/quotes")
}
:
form post values
和0 mytag1
1 mytag2
会产生类似的结果:
quote details
报价模型是这样的:
{ObjectIdHex("") some-author somebody [] }
因此接收标签值但未绑定。 我很想知道如何解决这个问题并从表单中获取标签? 我看过杜松子酒guid,却找不到任何关于这种形式的信息。
答案 0 :(得分:0)
问题出在模型结构中。
我忘了将Incident Identifier: 4A2BA2B1-9174-410F-9D51-88A151884484
CrashReporter Key: TODO
Hardware Model: iPhone8,1
Code Type: ARM-64
Parent Process: ??? [1]
Date/Time: 2016-09-28 02:43:26 +0000
OS Version: iPhone OS 10.0.2 (14A456)
Report Version: 104
Exception Type: SIGSEGV
Exception Codes: SEGV_ACCERR at 0x0
Crashed Thread: 21
Thread 21 Crashed:
0 CFNetwork _CFURLRequestCopyContentDispositionEncodingFallbackArray + 48
1 CFNetwork HTTPProtocol::updateForHeader(__CFHTTPMessage*) + 260
2 CFNetwork HTTPProtocol::performHeaderReadPostProcessing(__CFHTTPMessage*, unsigned char) + 156
3 CFNetwork HTTPProtocol::performHeaderRead(__CFHTTPMessage*) + 992
4 CFNetwork HTTPProtocol::handleStreamEvent(__CFHTTPMessage*, dispatch_data_s*, CFStreamError const*) + 496
5 CFNetwork ___ZN15HTTPTransaction22_onqueue_invokeHandlerEv_block_invoke.64 + 60
6 libdispatch.dylib _dispatch_client_callout + 16
7 libdispatch.dylib _dispatch_block_invoke_direct + 376
8 CFNetwork RunloopBlockContext::_invoke_block(void const*, void*) + 36
9 CoreFoundation CFArrayApplyFunction + 68
10 CFNetwork RunloopBlockContext::perform() + 136
11 CFNetwork MultiplexerSource::perform() + 312
12 CFNetwork MultiplexerSource::_perform(void*) + 64
13 CoreFoundation __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24
14 CoreFoundation __CFRunLoopDoSources0 + 524
15 CoreFoundation __CFRunLoopRun + 804
16 CoreFoundation CFRunLoopRunSpecific + 444
17 CFNetwork +[NSURLConnection(Loader) _resourceLoadLoop:] + 336
18 Foundation __NSThread__start__ + 1024
19 libsystem_pthread.dylib _pthread_body + 240
20 libsystem_pthread.dylib _pthread_body + 0
21 libsystem_pthread.dylib thread_start + 4
添加到模型中。所以标签没有绑定。