Group Group Group Group Group Group Group Group Group

Chapter 2: Routing with parameter

Just started on the book and already hit a hurdle when adding a route with a parameter. I installed Vapor via Homebrew exactly as described in the book, all went well. However the example in the books is:

router.get("hello", String.parameter) { req -> String in
	let name = try req.parameter(String.self)
	return "Hello, \(name)!"
}

This does not compile, given the error that ‘Request’ has no member ‘parameter’. Even the code in documentation on the Vapor website for version 3.0 gives this error. However when I look at the version 2.0 documentation, it would assign the name variable like this:

let name = try req.parameters.next(String.self)

This works! But I’m using Vapor 3.0. When I type “vapor version” in terminal, it reports as 3.0.0, and under the dependencies in Xcode it’s using Vapor 3.0.0, and the code hint comments say to do it this way and not the way in the book.

This was very recently changed in Vapor 3.0. In earlier alpha and beta versions of Vapor 3.0, req.parameter(String.self) really was the correct call but shortly before the final release, the call was changed back to req.parameters.next(String.self). I assume the book will reflect this change soon as well.

For now, you’re good with using req.parameters.next(String.self) and don’t worry, you’re definitely using the correct version of Vapor, even if the documentation for 2.0 is strangely enough more relevant for this particular method than the current state of documentation for 3.0.

There’s an update to the book coming this week (tomorrow hopefully) with all the changes in!

1 Like

app.get(“hello”, String.parameter) { req -> String in
let name = try req.parameters.next(String.self)

    return "Hello \(name)"
}

error: type ‘String’ has no member ‘parameter’
app.get(“hello”, String.parameter) { req -> String in
~~~~~~ ^~~~~~~~~
[1/2] Compiling App routes.swift

I’m using Swift 5.2 & vapor 4 on ubuntu

String.parameter is a Vapor 3 thing so looks like you’re conflating the two version. Take a look at the upgrade notes for how to convert and the newly updated tutorial for Vapor 4

https://www.raywenderlich.com/11555468-getting-started-with-server-side-swift-with-vapor-4

1 Like

Hey thank you so much!

Is that all what changed in vapor or we will get updated book?

@hassan20896 no there are some big changes, best summed up in the upgrade docs - https://docs.vapor.codes/4.0/upgrading/

There is a new book in progress, the early access release is almost finished