这是一个 Swift、Vapor 相关的问题。
在尝试创建 ResponseRepresentable 类时如何访问 FluentValue 类型?
Vapor Model
协议(protocol)需要一个 ID;我想将我的 ID 变量分配为可选的 FluentValue。但是,我找不到它(即使在 Import Vapor 和 Import Fluent 之后)。
import Vapor
import VaporMySQL
let drop = Droplet()
drop.post(String.self, "boughtcoffee") { request, buyerName in
let coffeeledger = CoffeeLedger(name: buyerName, time: 0)
guard let atLeastOneDrinker = request.data["drinker"].string else {
throw Abort.custom(status: .badRequest, message: "Please enter some drinkers")
}
return coffeeledger
}
进口 Vapor 导入流利
final class CoffeeLedger: Model {
var id: FluentValue?
var name: String
var time: Int
init (name: String, time: Int){
self.name = name
self.time = time
}
}
我正在关注 Vapor 视频,了解如何设置响应可表示的模型。 https://www.youtube.com/watch?v=E1ir7_s_VTw
最佳答案
FluentValue
在 Vapor 0.15 中变成了 Node
。 (此时 Vapor 的当前版本是 0.16)。
此外,init
和 serialize
方法变成了 init(node: ...)
和 makeNode()
.
模型页面上的代码 ( https://vapor.github.io/documentation/fluent/model.html ) 应该是最新的并且可以与 Vapor 0.15 及更高版本一起使用。
如果您遇到特定错误,请在此处使用该错误消息创建另一个问题,我会尽力提供帮助!
关于Vapor - 如何访问 FluentValue 类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38815353/