【发布时间】:2021-09-30 07:43:19
【问题描述】:
我的 Vapor 项目中有这个错误:
无法将“EventLoopFuture
”类型的返回表达式转换为“String”类型的返回
app.get("user", ":uuid") { req throws -> EventLoopFuture<String> in
let uuid = req.parameters.get("uuid") ?? ""
return User.query(on: req.db).filter(\.$uuid == uuid).first().flatMapThrowing { user in
if let user = user {
// return a string value
return try Response(data: ["user": user.makeDictionary()])
}else {
// not found, return all users
// problem is: Cannot convert return expression of type 'EventLoopFuture<String>' to return type 'String'
return User.query(on: req.db).all().map { $0.map { $0.makeDictionary() } }.flatMapThrowing { dicArray in
return try Response(data: ["users": dicArray])
}
}
}
}
我该如何解决?
【问题讨论】:
-
您是否尝试将
EventLoopFuture<String>替换为EventLoopFuture<Response>? -
我只是试试。但是解决不了。新错误:无法将“String”类型的返回表达式转换为“Response”类型,无法将“EventLoopFuture
”类型的返回表达式转换为“Response”类型 -
你只需要阅读关于 EventLoopFutures kirilltitov.com/en/blog/2019/futures的那篇好文章就可以了