1

I try to request some data from mySQL to render a table with leaf.

Because of the Futures I don't know how to combine FluentMySQL and Leaf.

import Vapor

final class IndexController {
    func index(_ req: Request) throws -> EventLoopFuture<View>{
        let futureEvents = try Event.query(on: req).all()

        // Here I want to add the querys results to 'renderParameters'
        var renderParameters = ...

        return try req.view().render("index", renderParameters)
    }
}
Nick
  • 4,820
  • 18
  • 31
  • 47
Andreas Prang
  • 2,187
  • 4
  • 22
  • 33

1 Answers1

2

It's difficult to give you an answer using the code in your question, but hopefully this will give you enough pointers to get you going.

Create a structure to hold the information you want to send to the view from your query results to the View. Make sure it is Encodable. I haven't got a simple example using Future, but something like the following could easily include [Future<Location>]

struct AllLocationsContext: Encodable {
    let title: String
    let hideInactiveLocations: Bool
    let locations: [Location]
}

Then add a flatMap to your query and embed both the creation of your parameters structure and the return that creates the View.

locations.get("/")
{
    request -> Future<View> in
    let title = "Events (Active and Inactive)"
    return Location.query(on:request).sort(\.name).all().flatMap(to: View.self)  {
        locations in
        let context = AllLocationsContext(title: title, hideInactiveLocations: hideInactiveLocations, locations: locations)
        return try request.view().render("locations",context)
    }
}
Nick
  • 4,820
  • 18
  • 31
  • 47