- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对如何查询模型对象的子对象并立即使用它感到困惑。我的Client
包含数量Station
child
final class Client: PostgreSQLModel {
var stations: Children<Client, Station> {
return children(\.clientID)
}
}
然后在我的 Controller 中,我有一个我想要查看并获取他们的电台的客户端列表。
func clientIndexHandler(_ req: Request) throws -> Future<View> {
return Client.query(on: req).all().flatMap(to: View.self) { clients in
let sorted = clients.sorted { ... }
let content = try sorted.map { client in
let stations = try client.stations
.query(on: req)
.all()
.map(to: [String].self) { stations in
return stations.map { $0.name }
}
// Now I want to use the stations for other stuff.
return ClientIndexContent(client: client, stations: stations, ....)
}
let context = ClientIndexContext(clients: content)
return try req.make(LeafRenderer.self).render("clients/index", context)
}
}
我的问题是stations
是 EventLoopFuture<[String]>
而不是[String]
。由于我在这里使用 Leaf,因此我需要来自客户端和工作站的实际值,以便我可以填充要传递到 Leaf 渲染器的内容。
最佳答案
所以你有很多方法可以做到这一点,但基本上你需要重新思考如何在异步世界中做事。然而,Vapor 确实提供了一些很好的东西来帮助实现这一点。首先,Leaf 实际上可以处理 future,所以如果你设置 ClientIndexContext
拥有 let stations: Future<[String]>
的属性,然后您就可以像平常一样访问 Leaf 内部的内容了。
您可以做的另一个选择是调用 map(to: [String].self)
上stations
这将为您带来所有的 future 。
关于fluent - 在 Fluent+Vapor 中查询子项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49992464/
在我的 Package.swift 文件中尝试更新版本后,我的 Vapor 和 Vapor-MySQL 设置失败了。 最终,我想要一个包含以下内容的 Package.swift: .Package(u
在 Vapor 3 中,我能够获取到数据库的 newConnection 并使用以下命令调用原始 sql 查询: return request.withNewConnection(to: .mysql
我有一个静态网页存储在我的 Vapor 服务器的公共(public)文件夹中。它有一个index.html 文件。但是,当我导航到 root (http://localhost:8080) 时,它显示
我有一个静态网页存储在我的 Vapor 服务器的公共(public)文件夹中。它有一个index.html 文件。但是,当我导航到 root (http://localhost:8080) 时,它显示
我关注了 Vapor tutorial创建一个你好应用程序。在 Xcode 中,当我在我的 Mac 上运行运行方案时,应用程序启动并运行,正如我通过打开 http://localhost:8080/看
我正在尝试使用存储库模式将一些代码从 Vapor 3 迁移到 Vapor 4。我已经完成了 documentation of this specific pattern来自 Vapor 4 文档,我想
我可以使用计时器吗,例如 NSTimer在 Vapor(服务器端 Swift)中? 我希望我用 Vapor 编写的服务器可以偶尔主动完成一些任务。例如,每 15 分钟从网络上轮询一些数据。 如何使用
我添加了 .package(url: "https://github.com/vapor-community/postgresql-provider.git", .upToNextMajor(from
我正在使用 Vapor 开发一个简单的 Web API。为了提供更多背景信息,我是后端开发的新手。 API 的使用者将是一个 iOS 应用程序。目前,我不需要用户注册即可使用该应用程序。我想保持这种状
在我的旧 Vapor 3 代码中,我正在创建这样的模型: address.create(on: conn).flatMap { savedAddress in // do something
我从 Vapor 3 更新到 4,从 Xcode 11 更新到 12。现在 Leaf 找不到我的 *.leaf 模板。我收到“noTemplateFile”错误。 最佳答案 我在这里找到了答案:htt
我有一个使用 get 处理程序的 Controller 返回 Future .我想在响应中添加一个 header (具体来说是 Cache-Control)。我在想这应该很容易,但我没有找到如何去做。
我想用最新的 vapor 3.x 构建 vapor 工具箱。 但是,vapor toolbox 的 Package.swift 仍然有 Vapor 2.x 依赖。 .Package(url: "htt
安装 Vapor 后,我遇到以下错误 dyld:未加载库:/usr/local/opt/openssl/lib/libssl.1.0.0.dylib 引用自:/usr/local/bin/vapor
当我尝试运行实现 block 读取和队列的导入时,我从 vapor 队列收到以下错误; ErrorException: touch(): Unable to create file /tmp/stor
我正在使用 Vapor 尝试从另一台服务器获取 XML 文件,问题是我不知道如何将响应正文转换为快速字符串。 let bikesResponse = try drop.client.get("http
我想创建一个命令,您可以在其中创建用户(如数据库种子)。 但是,我无法在命令中访问数据库,我的代码如下: import Command import Crypto struct CreateUserC
他们是否可以替代 Vapor 的 session 中间件包? 我们的客户端平台不支持 cookie。 Vapor 使用 cookie 进行 session 管理。 Redis 有一个 session
对于我的 Vapor 项目,我想主要使用一个 Postgres 数据库。但是,对于某些长时间运行的请求,我想使用同一数据库的单独只读克隆。 这方面的文档非常少。如何在现有默认数据库旁边添加另一个数据库
如何在登录方法中通过电子邮件在 vapor 中正确找到用户并返回该用户或返回错误,我试过: func login(_ req: Request) throws -> Future {
我是一名优秀的程序员,十分优秀!