Hi there,
getting my feet wet with Vapor 4 I want to serve several JSON files as raw Data or String for that matter using Vapor 4.
I have a database which contains besides other stuff UUIDs which also act as filenames for several JSON files.
Currently my code looks like this:
routes.swift:
func routes(_ app: Application) throws {
let geoJSONController = GeoJSONController()
…
app.get("MapEditorBackend", ":GeoJSONID", use: geoJSONController.get)
…
}
GeoJSONController.swift:
struct GeoJSONController {
…
func get(req: Request) throws -> EventLoopFuture<String> {
return GeoJSON.find(req.parameters.get("GeoJSONID"), on: req.db)
.unwrap(or: Abort(.notFound))
.flatMap { String(decoding: Data.fromFile($0.id!.uuidString), as: UTF8.self) }
.transform(to: .ok)
}
func delete(req: Request) throws -> EventLoopFuture<HTTPStatus> {
return GeoJSON.find(req.parameters.get("GeoJSONID"), on: req.db)
.unwrap(or: Abort(.notFound))
.flatMap { $0.delete(on: req.db) }
.transform(to: .ok)
}
}
when compiling above code I get those Errors:
SLPN-NB-LSH:MapEditorBackend lars$ ~/toolbox-18.0.0-beta.27/vapor-beta run
/Users/lars/Documents/Projects/MapEditor/MapEditorBackend/MapEditorBackend/Sources/App/Controllers/GeoJSONController.swift:16:14: error: generic parameter 'NewValue' could not be inferred
.unwrap(or: Abort(.notFound))
^
/Users/lars/Documents/Projects/MapEditor/MapEditorBackend/MapEditorBackend/.build/checkouts/swift-nio/Sources/NIO/EventLoopFuture.swift:465:17: note: in call to function 'flatMap(file:line:_:)'
public func flatMap<NewValue>(file: StaticString = #file, line: UInt = #line, _ callback: @escaping (Value) -> EventLoopFuture<NewValue>) -> EventLoopFuture<NewValue> {
^
/Users/lars/Documents/Projects/MapEditor/MapEditorBackend/MapEditorBackend/Sources/App/Controllers/GeoJSONController.swift:17:24: error: cannot convert value of type 'String' to closure result type 'EventLoopFuture<NewValue>'
.flatMap { String(decoding: Data.fromFile($0.id!.uuidString), as: UTF8.self) }
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Users/lars/Documents/Projects/MapEditor/MapEditorBackend/MapEditorBackend/Sources/App/Controllers/GeoJSONController.swift:18:29: error: type 'EventLoopFuture<String>' has no member 'ok'
.transform(to: .ok)
~^~
[2/3] Compiling App routes.swift
Fatal error: result 1: file /Users/lars/toolbox-18.0.0-beta.27/Sources/VaporToolbox/exec.swift, line 50
Illegal instruction: 4
while the delete
Function seems to work fine (I used it as a template for my get
function) here EventLoopFuture<String>
as a return type doesn't seem to work.
What I am doing wrong here? Do I have to wrap the String
I create in the flatMap
closure somehow?
Thanks in advance,
Lars