Grails groovy.lang.MissingMethodException。 shoppingCartService.addToShoppingCart(summary.items [partId] .item,delta)抛出MethodMissingException

时间:2013-10-25 16:28:29

标签: grails

def shoppingCartService

def produceShoppingCartInfo(){
    def shoppingItems = shoppingCartService.getItems()
    def summary = new ShoppingCartSummary(items: [:], total: BigDecimal.ZERO)
    shoppingItems.each { shoppingItem ->
        def part = Shoppable.findByShoppingItem(shoppingItem)
        def quantity = new BigDecimal(shoppingCartService.getQuantity(part))
        BigDecimal lineTotal = part.price.multiply(quantity)
        summary.items[part.id] = new Expando(quantity:quantity, item:part, lineTotal:lineTotal)
        summary.total = summary.total.add(lineTotal)
    }
    summary
}

def updateCartQuantities(newQuantities) {
    def summary =  produceShoppingCartInfo()
    newQuantities.each {  
        def partId = it.key
        def newQuantity = it.value
        if(summary.items[partId]) {
            def currentQuantity = summary.items[partId].quantity
            def delta = newQuantity - currentQuantity
            if(delta > 0) {
                shoppingCartService.addToShoppingCart(summary.items[partId].item, delta)
//              shoppingCartService.addToShoppingCart(summary.items[partId].item)
            } else if (delta < 0) {
                shoppingCartService.removeFromShoppingCart(summary.items[partId].item, Math.abs(delta))
//              shoppingCartService.removeFromShoppingCart(summary.items[partId].item)
            }
        }
    }
    produceShoppingCartInfo()

}

def getTotalItemsForCart(){
    def summary =  produceShoppingCartInfo()
    def totalItems = 0

    summary.items.each { partId, itemInfo  ->
        def currentItemQuantity = itemInfo.quantity
        totalItems = totalItems+currentItemQuantity
    }

    totalItems

}

1 个答案:

答案 0 :(得分:0)

这意味着您没有将正确的参数传递给addToShoppingCart。它需要IShoppableInteger

def addToShoppingCart(IShoppable product, Integer qty = 1, ShoppingCart previousShoppingCart = null)

您的域类是否实现了IShoppable?您是否将域实例作为第一个参数传递?

插件服务的来源here

修改

您的问题可能与您的变量类型有关。在这种情况下,最好使用具体类型而不是Groovy def

def delta = newQuantity - currentQuantity
println delta.class.name //here you will see if this is really a Integer

您可能需要以下内容:

Integer delta = newQuantity.asInteger() - currentQuantity.asInteger()

仅当您不需要知道对象的类型时才选择使用def。如果您知道这是LongIntegerBigDecimal等,则倾向于使用类型声明您的变量。