WWDC23 SALE: Save 50% on all my Swift books and bundles! >>

Server-Side Swift Vapor Editon, project 2, code update for Swift 5.2 on pp351

Forums > Books

In project2 (page 351 in the PDF) the code from the book introduces a fatal error that won't let the server run. I tried running the code from the sample files and it produces the same error.

var migrationConfig = MigrationConfig()
migrationConfig.add(model: Poll.self, database: .sqlite)
services.register(migrationConfig)

the migrationConfig.add line introduces a fatal error that highlights .sqlite and says "Reference to member 'sqlite' cannot be resolved without a contextual type"

Am I missing some sort of dependecy to make it work? everything worked fine up until this step.

2      

I found a place on the internets (over at the raywenderlich forums) that suggests it's a new thing with Swift 5.2, so the book isn't really in error, Swift is just constantly evolving.

the post said to "provide the full database identifier" which turns out to be

DatabaseIdentifier<Poll.Database>.sqlite

So those three lines above become

    var migrationConfig = MigrationConfig()
    migrationConfig.add(model: Poll.self, database: DatabaseIdentifier<Poll.Database>.sqlite)
    services.register(migrationConfig)

and that appears to fix the problem. Everything compiles now, back to learnin... :)

1      

Hi, I had the same issue and this post helped. Thanks. I am not getting a response on the create curl line:

curl -X POST localhost:8080/polls/create -d "title=What's the best color?" -d "option1=Jet Black" -d "option2=Rose Gold" -d "votes1=0" -d "votes2=0”

I tried removing the "http://", made sure that I didn't misspell any vars in the Poll struct. Also, I tried it on the project2 file from Paul's projects. Nothing. I am very inexperienced with the command line tool and just not sure if I am doing it right.

Any help appreciated?

2      

I had a problem with project2 as well. On page 158 the book instructed to build and run. Got a "build successful" but nothing after that. Didn't get "Server starting on http://localhost:8080" on console.

Checked the code several times... no luck. Deleted and rewrote the whole thing again. Still nothing.

Help!

Edit: I found the problem. I forgot to change the default scheme.

Went to menu Product>Scheme.. was set on “project2-Package”. Changed it to “Run”. Now it works!! This was explained in the first project of the book but wasn’t mentioned again in project2. I didn’t realize it.

1      

Save 50% in my WWDC23 sale.

SAVE 50% To celebrate WWDC23, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.