gpt4 book ai didi

ios - Fluent.EntityError 错误 0

转载 作者:行者123 更新时间:2023-11-28 06:25:32 24 4
gpt4 key购买 nike

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

所以这是我的代码:耐心.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")
}

}

主.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,我会得到一个错误: enter image description here

教程并没有谈到首先在数据库中创建一个表,但这就是 prepare 函数的内容,对吗?还有什么问题吗?

mysql.json

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

最佳答案

我认为这是与命名转换有关的问题。在 postman 中,您发送名为“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]

关于ios - Fluent.EntityError 错误 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41994883/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com