Server Side Swift with Vapor - Part 4: Accepting Data | Ray Wenderlich

Worked like a charm, thanks!

1 Like

@0xtim I receive a Use of undeclared type "InfoResponse" compiler error when using the new way.

@gsequeira do you have an InfoResponse struct yet in your code? If you are on the first part and you returning a string it would look like:

router.post(InfoData.self, at: "info") { req, data -> String in
  return "Hello \(data.name)!"
}
3 Likes

Still on the first part. Thanks.

1 Like

This has been helpful, maybe Ray Wenderlich team could implement annotations that point to these comments in cases like these ?

2 Likes

That would be awesome.

This gist has all the code changes caused by the recent update.

1 Like

I still dont get the code in CRUD operations working after installing the latest version of Vapor 3.0.0 and the changes suggested here

Please help

What errors are you getting? I’m currrntly re-recording the videos so updated ones should be available in the new year!

Also check out GitHub - raywenderlich/vapor-til: The TIL Application for the Vapor book which is from the book, but builds on everything from the videos. Should help point you in the right direction

For example, I havent been able to implement the updateHandler method as described in vapor-til repo from git hub. But I implemented it the followign way that returns the Acronym that is updated instead of HTTPStatus. I couldnt get the flapMap to return HTTP Status.

func updateHandler(_ req: Request) throws → Future {
return try flatMap(to: Acronym.self, req.parameters.next(Acronym.self), req.content.decode(Acronym.self)) { acronym, updateData in
acronym.short = updateData.short
acronym.long = updateData.long
return acronym.save(on: req)
}
}

Now I m stuck in searchHandler. The code given in the repo complains “Type of expression is ambiguous without more context” build error for the query method with filter. I am struggling to fix that.

So if you’re returning HTTPStatus you’d need to do something like:

func updateHandler(_ req: Request) throws -> Future<HTTPStatus> {
  return try flatMap(to: HTTPStatus.self, req.parameters.next(Acronym.self),  
                                 req.content.decode(Acronym.self)) { acronym, updateData in
    acronym.short = updateData.short
    acronym.long = updateData.long
    return acronym.save(on: req).transform(to: .ok)
  }
}

For the search handler, have you imported Fluent?

Thanks, the import was the issue for search Handler. However I am now stuck in MySQL when the FLuentMySQL is giving build errors. Can you please let me now if I am missing any configuration? I have set the -DNOJSON as instructed in some other thread in stack overflow but no luck

/FluentMySQL/MySQLType.swift:3:30: Use of undeclared type ‘MySQLDataTypeStaticRepresentable’

Try running swift package update to get the latest code and doing a clean

Hi Tim, Thanks for the info, the update solved my problems but for some strange reason the connection is not allowed to the MySQL server created by docker.

What error are you getting?