Reputation: 1924
I'm new to vapor and I'm stucked. I'm following this tutorial: https://www.xplusz.com/how-to-do-basic-crud/ but I'm using a MySQL database instead of a postgresql database.
So this is the code I have: Patient.swift
import Foundation
import Vapor
import Fluent
final class Patient: Model {
var id: Node?
var firstName: String
var lastName: String
var exists: Bool = false
init(firstName: String, lastName: String) {
self.id = nil
self.firstName = firstName
self.lastName = lastName
}
init(node: Node, in context: Context) throws {
id = try node.extract("id")
firstName = try node.extract("first_name")
lastName = try node.extract("last_name")
}
func makeNode(context: Context) throws -> Node {
return try Node(node: [
"id": id,
"first_name": firstName,
"last_name": lastName
])
}
static func prepare(_ database: Database) throws {
try database.create("patients") { users in
users.id()
users.string("first_name")
users.string("last_name")
}
}
static func revert(_ database: Database) throws {
try database.delete("patients")
}
}
main.swift
import Vapor
import VaporMySQL
let drop = Droplet(
providers:[VaporMySQL.Provider.self]
)
drop.get { req in
return try drop.view.make("welcome", [
"message": drop.localization[req.lang, "welcome", "title"]
])
}
drop.get("version"){request in
if let db = drop.database?.driver as? MySQLDriver{
let version = try db.raw("SELECT version()")
return try JSON(node: version)
}else{
return "No db connection"
}
}
drop.post("patients") { request in
print(request.json)
var patient = try Patient(node: request.json)
try patient.save()
return patient
}
drop.get("patients"){request in
return try JSON(node: Patient.all().makeNode())
}
drop.resource("posts", PostController())
drop.run()
If I run version, everything works fine:
[
{
"version()": "5.7.14"
}
]
But for example if I run post or get requests, I get an error:
The tutorial didn't talk about creating a table in the database first, but thats what the prepare function is about right? What else is wrong?
mysql.json
{
"host": "localhost",
"user": "root",
"password": "",
"database": "development",
"port": "3306",
"encoding": "utf8"
}
Upvotes: 1
Views: 229
Reputation: 3798
I think this is a issue related to naming conversion. In postman you are sending parameters named "firstName" & "lastName" and in your model class you are using parameters which are underscore separated like "first_name" & "last_name", you should follow same naming conversion.
Update:-
Try in below way and let me know if you are still facing this error.
guard let first_name = request.data["first_name"]?.string, let last_name = request.data["last_name"]?.string else {
return try JSON(node:[
"error": true,
"message": "first_name or last_name not found"
])
}
var patient = Patient(firstName: first_name, lastName: last_name)
do {
try patient.save()
return try JSON(node:[
"error" : false,
"message" : "patient added successfully"
])
} catch let error{
return try JSON(node:[
"error" : true,
"message" : "\(error)"
])
}
Update 2:-
One should add provider and prepare data base, before use.
try drop.addProvider(VaporMySQL.Provider.self)
drop.preparations = [Patient.self]
Upvotes: 1