1

我是蒸汽新手,我被困住了。我正在关注本教程:https ://www.xplusz.com/how-to-do-basic-crud/但我使用的是 MySQL 数据库而不是 postgresql 数据库。

所以这是我的代码: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()

如果我运行版本,一切正常:

[
  {
    "version()": "5.7.14"
  }
]

但例如,如果我运行 post 或 get requests,我会收到一个错误: 在此处输入图像描述

教程并没有首先讨论在数据库中创建表,但这就是准备函数的内容吧?还有什么问题?

mysql.json

{
  "host": "localhost",
  "user": "root",
  "password": "",
  "database": "development",
  "port": "3306",
  "encoding": "utf8"
}
4

1 回答 1

1

我认为这是与命名转换有关的问题。在邮递员中,您发送名为“ firstName ”和“ lastName ”的参数,而在您的模型类中,您使用的是下划线分隔的参数,如“ first_name ”和“ last_name ”,您应该遵循相同的命名转换。

更新:-

请尝试以下方式,如果您仍然面临此错误,请告诉我。

    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)"
            ])
    }

更新2:-

使用前应添加提供者并准备数据库。

try drop.addProvider(VaporMySQL.Provider.self)
drop.preparations = [Patient.self]
于 2017-02-02T07:33:06.433 回答