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

In this video, you'll learn how to accept data in your Vapor applications and parse it with Codable.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/4493-server-side-swift-with-vapor/lessons/4
1 Like
struct InfoData: Content {
    let name: String
}

Non-class type 'InfoData' cannot inherit from class 'Content'

@0xtim Can you please help with this when you get a chance? Thank you - much appreciated! :]

@valveriy_kliuk have you got a class called Content declared anywhere? If you click through to the definition in Xcode where does it think it’s declared? Also check that you have import Vapor in the file

hello , can you help me? please

when definition struct
i have this error

struct InfoResponse : Content {
let name : String
}
First Error : Non-class type ‘InfoResponse’ cannot inherit from class ‘Content’

and when definition return value " req -> InfoResponse "
i have this error
Second Error : Unable to infer closure type in the current context

in the documentation vapor

https://docs.vapor.codes/

i can’t find your ways to defines router (such as work with Content , or etc ) in this lesson

@lashkari it looks like you are on Vapor 2. Please ensure you use the --branch=beta flag when you create the project

The documentation for Vapor 3 is https://docs.vapor.codes/3.0/

Note that it is quite out of date at the moment as the beta has evolved

@valveriy_kliuk make sure you passed in the --branch=beta when you created the project

when use --branch=beta i have errors in Xcode

@lashkari what are the errors you are getting? Have you got Swift 4.1?

oh sorry i don’t use this

ok, thanks download and install this now

No worries :slightly_smiling_face:

Instructions for installing the Swift 4.1 toolchain are in Video 2

Sorry
thanks a lot :pray:

1 Like

Np problem here. I have import Vapor

I did’t passe it. Thanks!

1 Like

The app is crashing when making the post request. “wait() must not be called when on the EventLoop” is the error. Any help?

I think this is an NIO issue. I guess it was just implemented into Vapor today or yesterday, and it includes a precondition against this. Not sure if it has to do with the deprecation of await too? I got errors when trying to use wait () instead of await(). Using up-to-date/beta versions of everything: Swift 4.1, Xcode 9.3 beta4 , and Vapor 3.1.4.l --see 1 minute video for the errors: https://drive.google.com/open?id=1C-muv2iSg8njvdOISXxFnZbpeRXm2zQv

I got it to work by commenting out this precondition in the EventLoopFuture.swift file (temporarily for the exercise only of course):

// if !(self.eventLoop is EmbeddedEventLoop) {
// precondition(!eventLoop.inEventLoop, “wait() must not be called when on the EventLoop”)
// }

@amyerson @trouge the new way of doing it is:

router.post(InfoData.self, at: "info") { req, data -> InfoResponse in
  return InfoResponse(request: data)
}

Apologies, originally the wait() call should have worked!

3 Likes