Search code examples
grailsgrails-domain-classgrails-controllergrails-services

MissingPropertyException in grails


I have a problem when I try to show the view of one of my domains. I built a method to sum some quantities, but I have this error and I don't know how to fix it. I understand that the variable awards does not exist, but I have this value in my domain. Here is the code of the only method in the controller that gives me the error.

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: 

CONTROLLER (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]
    }

MODEL COSTUMER

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)
    }
}

MODEL AWARD

package rewards

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

    static constraints = {
    }
}

SERVICES (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
    }
}

Solution

  • You've spelled hasMany wrong in your Customer domain.

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

    should be

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