控制器操作无法使用"类型不匹配找到:所需单位:play.api.mvc.Result"

时间:2012-05-05 15:14:22

标签: scala playframework contacts exchangewebservices

我是Scala和Play框架的新手。我必须创建一个“CONTACTS”模块,但是我收到以下错误:

type mismatch found : Unit required: play.api.mvc.Result 
    contactVal.save()
where contactVal is defined as 
    val contactVal  = new Contact(service) where service is 
    val service = new ExchangeService()

如何保存新的EWS联系人?

我的代码:

def add = Action(parse.json) {
  implicit r=>
    val contactVal  = new Contact(service)
    val userId = (r.body \ "userId").asOpt[String].getOrElse("")
    val contactId = (r.body \ "id").asOpt[String].getOrElse("")
    val givenName = (r.body \ "givenName").asOpt[String].getOrElse("")
    val fName = (r.body \ "fName").asOpt[String].getOrElse("")
    val lName = (r.body \ "lName").asOpt[String].getOrElse("")
    val displayName = (r.body \ "displayName").asOpt[String].getOrElse("")
    val emailId1 = (r.body \ "emailId1").asOpt[String].getOrElse("")

    val streetB = (r.body \ "streetB").asOpt[String].getOrElse("")
    val cityB = (r.body \ "cityB").asOpt[String].getOrElse("")
    val stateB = (r.body \ "stateB").asOpt[String].getOrElse("")
    val postalcodeB = (r.body \ "postalcodeB").asOpt[String].getOrElse("")
    val countryB = (r.body \ "countryB").asOpt[String].getOrElse("")
    val phoneHome = (r.body \ "phoneHome").asOpt[Int].getOrElse("")
    val bday = (r.body \ "bday").asOpt[String].getOrElse("")

    contactVal.setGivenName(givenName)
    contactVal.setNickName(fName)
    contactVal.setSurname(lName)
    contactVal.setDisplayName(displayName)

    val bdayDate= new Date()
    bdayDate.setDate(bday.toInt)
    contactVal.setBirthday(bdayDate)

    contactVal.save()
}

1 个答案:

答案 0 :(得分:6)

问题是Action主体中的最后一行代码没有返回play.api.mvc.Result对象。

请参阅:http://www.playframework.org/documentation/2.0.1/ScalaActions

尝试在操作体的末尾添加Ok(或其他一些SimpleResult对象)。例如:

def add = Action(parse.json) { request =>
  ...
  contactVal.save()
  Ok("contact saved") // or if you want to render a templated: Ok(someTemplate())
}