gpt4 book ai didi

Grails - 无法将子记录添加到父记录

转载 作者:行者123 更新时间:2023-12-02 15:34:32 24 4
gpt4 key购买 nike

尝试按照显示的示例 here我正在尝试创建一个链接到父记录的记录。

就我而言,我有两个类:传感器和读数。我可以毫无问题地创建传感器,但无论我如何尝试创建读数,我似乎都失败了:(

我已经旋转轮子足够长的时间了,我认输了,希望有人能发现我的愚蠢错误。

还有一件事 - 我想使用 JSON 发布数据。但是通过调试过程,我什至没有查看 JSON 值,我已经对它们进行了硬编码,但它仍然无法正常工作。

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)


}
}

传感器.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: &quot;SID&quot;, sensorname: &quot;name&quot;, sensordescription: &quot;description&quot;)</code><code class="line"><span class="lineNumber">16:</span> .addToReadings(reading: &quot;blah&quot;)</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>

最佳答案

您是否尝试过显式创建一个新的 Readings ?

def s = new Sensor (sensorid: "SID", sensorname: "name", sensordescription: "description")
.addToReadings(new Readings(reading: 'blah'))
.save()

错误是“第 15 行附近”,这是 def s = ... 的开始。陈述。

我知道文档说它可以按照您尝试的方式完成 - 但值得一试。

关于Grails - 无法将子记录添加到父记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13886331/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com