如何从 Vapor 中的数组制作 "future"?
How to make "future" from an array in Vapor?
有以下代码:
router.get("/fetchOngoingReleases", String.parameter) { (request) -> Future<[ReleaseWithUser]> in
return Release.query(on: request).filter(\.inProgress == true).all().flatMap { (ra) -> EventLoopFuture<[ReleaseWithUser]> in
let userId = try request.parameters.next(String.self)
return User.query(on: request).filter(\.fbId == userId).first().flatMap { (user) -> EventLoopFuture<[ReleaseWithUser]> in
let a = ra.map { (r) -> ReleaseWithUser in
// some condition and logic here
}
return a. //need this to make future
}
}
}
我检查了所有可能的方法调用,但 none of 将 [ReleaseWithUser]
转换为 Future<[ReleaseWithUser]>
。你有什么想法吗?
解决方案是:
ra.map { (r) -> ReleaseWithUser in
// some condition and logic here
}.flatten(on: request)
数组可以通过flatten()
转换为未来的数组。
有以下代码:
router.get("/fetchOngoingReleases", String.parameter) { (request) -> Future<[ReleaseWithUser]> in
return Release.query(on: request).filter(\.inProgress == true).all().flatMap { (ra) -> EventLoopFuture<[ReleaseWithUser]> in
let userId = try request.parameters.next(String.self)
return User.query(on: request).filter(\.fbId == userId).first().flatMap { (user) -> EventLoopFuture<[ReleaseWithUser]> in
let a = ra.map { (r) -> ReleaseWithUser in
// some condition and logic here
}
return a. //need this to make future
}
}
}
我检查了所有可能的方法调用,但 none of 将 [ReleaseWithUser]
转换为 Future<[ReleaseWithUser]>
。你有什么想法吗?
解决方案是:
ra.map { (r) -> ReleaseWithUser in
// some condition and logic here
}.flatten(on: request)
数组可以通过flatten()
转换为未来的数组。