3

我是 Scala 和 Play 框架的新手。我必须创建一个“联系人”模块,但出现以下错误:

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

1 回答 1

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())
}
于 2012-05-05T17:08:08.183 回答