makeNode(in: Context) vapor 3 中的上下文是什么?

What is context in makeNode(in: Context) vapor 3?

将 Vapor 用于 return 模型到节点:

 func indexView(request: Request) throws -> ResponseRepresentable
    {
        let acro = try Acronym.makeQuery().sort(Acronym.idKey, .ascending)

        return try acro.all().makeNode(in: <#T##Context?#>)
    }

它总是 return 错误并且不知道如何修复它。

上下文通常用于在 Vapor 内部传递 current-use 信息。 Vapor 3 文档似乎没有包含详细信息(还),但请参阅 Vapor 2 的 https://docs.vapor.codes/2.0/node/getting-started/。我从未在 makeNode 中发现需要它们,因此放置:

return try acro.all().makeNode(in:nil)

应该可以正常工作。

Node 已在 Vapor3 中被淘汰。不需要它,因为它依赖于 Swift 的本机数据类型+协议。在这种情况下,Vapor 3 使用 Content 来处理 JSON。 (内容默认符合Codable)

例如

final class Person: Content, MySQLModel, Migration {
    var id: Int?
    var name: String

    init(id: Int? = nil, name: String) {
        self.id = id
        self.name = name
    }

}

func person(_ req: Request) throws -> Future<Person> {
    return Person(name: "Mark")
}

router.get("test") { req -> Future<[Person]> in
    return try Person.query(on: req).sort(\.id, .ascending).all()
}

文档摘录:

In Vapor 3, all content types (JSON, protobuf, URLEncodedForm, Multipart, etc) are treated the same. All you need to parse and serialize content is a Codable class or struct.