简体   繁体   中英

Why do I get LocalDateTime error in Scala?

Im trying to build a small web page for my school project in scala with cask. Im trying to use the LocalDateTime data type in my forms and in the post handlers. For some reason I get this error when trying to run the website:

could not find implicit value for parameter p: annotObject$macro$24.InputParser[java.time.LocalDateTime]

[error]  def f (name: String, location: String, team: String, begins: LocalDateTime, ends: LocalDateTime, info: String) = {

I have no clue what's going on, any help is appreciated. This is my code:

import backend.Session
import backend.test.Test._
import scalatags.Text.all.{borderWidth, link, marginBottom, p, _}

import java.time.LocalDateTime

object MinimalApplication extends cask.MainRoutes{

  basicUser()

  var session = new Session(None, false)


  @cask.postForm("/createEvent")
 def f (name: String, location: String, team: String, begins: LocalDateTime, ends: LocalDateTime, info: String) = {
    createEvent.post(name, location, team, begins, ends, info)
  }


  @cask.get("/createEvent")
  def e () = createEvent.get()


  initialize()
}

And the handlers look like this:

import scalatags.Text.all.{form, p, _}

import java.time.LocalDateTime

object createEvent {

  def get() = {

    html(
      head(
        link(
          rel := "stylesheet",
          href := "https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
        ),

      ),
      body(
        div(width := "50%",height := "100vh", display := "flex", flexDirection := "column", padding := "50px", borderWidth :="3px", borderStyle:="solid", borderColor := "black", marginLeft := "25%")(
          h1(marginBottom:="100px", alignSelf := "center")("Create a new event"),
          form(action := "/createEvent", method := "post")(
            div(width := "100%", height := "100%", display := "flex", justifyContent := "center", alignItems := "flex-start", flexDirection := "column", alignSelf:="center")(
            input(
              marginBottom := "10px",
              `type` := "text",
              name := "name",
              placeholder := "Event name",
              width := "70%",
              borderWidth := "2px",
              borderStyle := "solid",
              backgroundColor := "White",
              borderColor := "(240, 240, 240)",
              borderRadius := "5px"
             // userName.map(value := _)
            ),
            input(
              `type` := "text",
              name := "location",
              placeholder := "Location",
              width := "50%",
              borderWidth := "2px",
              borderStyle := "solid",
              backgroundColor := "White",
              borderColor := "(240, 240, 240)",
              borderRadius := "5px",
              marginBottom := "10px"
             // msg.map(value := _)
            )
              ,
            input(
              `type` := "text",
              name := "team",
              placeholder := "Team",
              width := "25%",
              borderWidth := "2px",
              borderStyle := "solid",
              backgroundColor := "White",
              borderColor := "(240, 240, 240)",
              borderRadius := "5px",
              marginBottom := "10px"
             // msg.map(value := _)
            ),
              div(display := "flex")(
              div(display:= "flex", flexDirection := "column", marginRight := "20px")(
                h6()("Begins"),
                input(
              `type` :="datetime-local",
                min:="2021-11-12T19:30",
                  value := s"${LocalDateTime.now()}",
              name := "begins",
              width := "100%",
              height := "30px",
              borderWidth := "2px",
              borderStyle := "solid",
              backgroundColor := "White",
              borderColor := "(240, 240, 240)",
              borderRadius := "5px",
              marginBottom := "10px"
                )
              ),
                div(display:= "flex", flexDirection := "column")(
                h6()("Ends"),
                input(
              `type` :="datetime-local",
                min:="2021-11-12T19:30",
               value := s"${LocalDateTime.now()}",
              name := "ends",
              width := "100%",
              height := "30px",
              borderWidth := "2px",
              borderStyle := "solid",
              backgroundColor := "White",
              borderColor := "(240, 240, 240)",
              borderRadius := "5px",
              marginBottom := "10px"
                )
              )
            ),
            textarea(
              `type` := "text",
              name := "info",
              placeholder := "Info",
              width := "70%",
              height := "150px",
              borderWidth := "2px",
              borderStyle := "solid",
              backgroundColor := "White",
              borderColor := "(240, 240, 240)",
              borderRadius := "5px",
              marginBottom := "10px"
             // msg.map(value := _)
            ),

            button(
              alignSelf := "center",
              `type` := "submit",
              width := "15%",
              height := "30px",
              marginTop := "20px",
              borderWidth := "1px",
              borderStyle := "solid",
              backgroundColor := "light-grey",
              borderColor := "Black",
              borderRadius := "5px")("Create")


          )
          )

        )

      )
    )

  }

  def post(name: String, location: String, team: String, begins: LocalDateTime, ends: LocalDateTime, info: String) = {
   if(name.nonEmpty && location.nonEmpty && team.nonEmpty) {
     html(
       head(),
       body(
         p()(s"nimi: ${name}"),
         p()(s"paikka: ${location}"),
         p()(s"tiimi: ${team}"),
         p()(s"alkaa: ${begins}"),
         p()(s"loppuu: ${ends}"),
         p()(s"info: ${info}")
       )
     )
   }
  }

}

could not find implicit value for parameter p: InputParser[java.time.LocalDateTime]

You get the error because Cask doesn't know how to read a LocalDateTime from a request.

One way to solve the issue is to implement a implicit val reader: InputParser[LocalDateTime] = ??? , or maybe Cask provides it through some other library or imports?

Another way is to read the parameters as regular String s and do the parsing yourself in the f method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM