試圖按照所示的示例here我試圖創建一個鏈接到父記錄的記錄。Grails - 不能添加子記錄到父
在我的情況下,我有兩個類:傳感器和讀物。我可以在沒有任何問題的情況下創建傳感器,但無論我如何嘗試創建讀數,我似乎都失敗:(
我一直在旋轉我的輪子足夠長時間,我在扔毛巾,希望有人能夠發現我的愚蠢錯誤
還有一件事 - 我想使用JSON發佈數據,但通過調試過程我甚至沒有看JSON值,我已經硬編碼它們,它仍然沒有「T工作。
ReadingsController.groovy
package grailshelloworld
import grails.converters.JSON
import groovy.json.JsonSlurper
class ReadingsController {
def scaffold=Readings
def save = {
def slurper = new JsonSlurper()
def result = slurper.parseText(request.reader.text)
def s = new Sensor (sensorid: "SID", sensorname: "name", sensordescription: "description")
.addToReadings(reading: "blah")
.save()
render ([ok: false] as JSON)
}
}
sensor.groovy
package grailshelloworld
class Sensor {
String sensorid
String sensorname
String sensordescription
static hasMany = [readings: Readings]
static constraints = {
sensorid blank:false, nullable: false
sensorname blank:false, nullable: false
}
}
Readings.grooovy
package grailshelloworld
import java.util.Formatter.DateTime;
class Readings {
String reading
static belongsTo = [sensor: Sensor]
}
當前錯誤:參數類型不匹配......
<dt>Class</dt><dd>java.lang.IllegalArgumentException</dd><dt>Message</dt><dd>argument type mismatch</dd></dl><h2>Around line 15 of <span class="filename">grails-app/controllers/grailshelloworld/ReadingsController.groovy</span></h2>
<pre class="snippet"><code class="line"><span class="lineNumber">12:</span> def slurper = new JsonSlurper()</code><code class="line"><span class="lineNumber">13:</span> def result = slurper.parseText(request.reader.text)</code><code class="line"><span class="lineNumber">14:</span></code><code class="line error"><span class="lineNumber">15:</span> def s = new Sensor (sensorid: "SID", sensorname: "name", sensordescription: "description")</code><code class="line"><span class="lineNumber">16:</span> .addToReadings(reading: "blah")</code><code class="line"><span class="lineNumber">17:</span> .save()</code><code class="line"><span class="lineNumber">18:</span></code></pre><h2>Around line 195 of <span class="filename">PageFragmentCachingFilter.java</span></h2>
不是'addToReadings'將父項添加到父項?你把它叫做一個父母的傳感器。也許你應該創建'Readings'並將其添加到'Sensor'? – nidu
你能告訴我們你的JSON看起來像什麼嗎?由於錯誤出現在您的parseText調用中,因此可能是JSON的問題。 – Gregg
@nidu - 代碼甚至在引發錯誤之前還沒有到達。只要具有閱讀的閱讀:「blah」實際存在,grails就允許使用該語法。 – Gregg