Argh, I've just run up against the limits of my understanding of the Swift compiler's error messages and Swift syntax. Right at the end of project 2, we're building the voting endpoint. This is totally clear, except that this code:
app.post("polls", "vote", ":id", ":option") { req -> EventLoopFuture<Poll> in
let pollID = req.parameters.get("id", as: UUID.self)
let option = req.parameters.get("option", as: Int.self)
return try Poll.find(pollID, on: req.db).flatMap(to: Poll.self) { poll in
guard var poll = poll else {
throw Abort(.notFound)
}
if option == 1 {
poll.votes1 += 1
} else {
poll.votes2 += 1
}
return poll.save(on: req.db)
}
}
generates a compiler error on the line with the flatMap
call. The error is: Cannot convert value of type 'Poll.Type' to expected argument type '(Poll?) -> EventLoopFuture<Poll>'
I've been trying different type declarations and different ways of declaring that trailing closure, but all that I've achieved is getting slightly different compiler errors and they all still complain about not being able to transform the closure's signature. Any help?