【问题标题】:MissingPropertyException in grailsgrails 中的 MissingPropertyException
【发布时间】:2015-04-29 14:31:09
【问题描述】:

当我尝试显示我的一个域的视图时遇到问题。我建立了一个方法来总结一些数量,但我有这个错误,我不知道如何解决它。我知道可变奖励不存在,但我在我的域中有这个值。这是控制器中唯一给我错误的方法的代码。

URI /Rewards/customer/show/1
Class groovy.lang.MissingPropertyException
Message No such property: awards for class: rewards.Customer

Around line 14 of grails-app/services/rewards/CalculationsService.groovy
11: 
12: def getTotalPoints(customerInstance){
13:     def totalAwards = 0
14:     customerInstance.awards.each{
15:         totalAwards = totalAwards + it.points
16:     }
17:     customerInstance.totalPoints = totalAwards

Around line 33 of grails-app/controllers/rewards/CustomerController.groovy
30: 
31: def show(Long id){
32:     def customerInstance = Customer.get(id)
33:     customerInstance = calculationsService.getTotalPoints(customerInstance)
34:     [customerInstance: customerInstance]
35: }
36: 

控制器(CustomerController.groovy)

package rewards

class CustomerController {
    static scaffold = true

    def calculationsService

    def show(Long id){
        def customerInstance = Customer.get(id)
        customerInstance = calculationsService.getTotalPoints(customerInstance)
        [customerInstance: customerInstance]
    }

模特儿

class Customer {

    String firstName
    String lastName
    Long phone
    String email
    Integer totalPoints
    static hastMany = [awards:Award, orders:OnlineOrder]

    static constraints = {
        phone()
        firstName(nullable: true)
        lastName(nullable: true)
        email(nullable: true, email: true)
        totalPoints(nullable: true)
    }
}

模特奖

package rewards

class Award {
    Date awardDate
    String type
    Integer points
    static belongsTo = [customer:Customer]

    static constraints = {
    }
}

服务(CalculationsService.groovy)

package rewards

import grails.transaction.Transactional

@Transactional
class CalculationsService {


    def serviceMethod() {

    }

    def getTotalPoints(customerInstance){
        def totalAwards = 0
        customerInstance.awards.each{
            totalAwards = totalAwards + it.points
        }
        customerInstance.totalPoints = totalAwards
        return customerInstance
    }
}

【问题讨论】:

  • 能否提供型号Customer的代码?
  • MODEL COSTUMER class Customer { String firstName String lastName Long phone String email Integer totalPoints static hastMany = [awards:Award, orders:OnlineOrder] 静态约束 = { phone() firstName(nullable: true) lastName(可空:真)电子邮件(可空:真,电子邮件:真)总点数(可空:真)}}
  • 尝试运行 grails clean 看看是否有帮助
  • 不,我清理了项目,但我有同样的错误
  • 您最近是否添加了“awards”属性?您是否有机会在没有此属性的情况下在数据库中插入客户?如果是这样,您需要清理数据库

标签: grails grails-domain-class grails-controller grails-services


【解决方案1】:

您在客户域中拼写错误hasMany

static hastMany = [awards:Award, orders:OnlineOrder]

应该是

static hasMany = [awards:Award, orders:OnlineOrder]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-15
    • 1970-01-01
    • 2012-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-07
    相关资源
    最近更新 更多