将 EventLoopFuture<Type> 映射到预期类型时出错
Error when mapping an EventLoopFuture<Type> to an expected Type
我尝试过映射、平面映射、创建 Post
的 EventLoopFuture
等...我正在尝试 return 两个 db.queries
创建要在 req.view.render
上呈现的单独内容数组,其中 return 是 EventLoopFuture<View>
。
我的错误是:Cannot convert value of type 'EventLoopFuture<[Posts]>' to expected argument type '[Posts]'.
这是路由器调用的我的代码:
func blogView(req: Request) throws -> EventLoopFuture<View> {
struct PageContext: Encodable {
var posts: [Posts]
var categories: [Categories]
var title: String
}
struct Posts: Encodable {
var posts: BlogPostModel.ViewContext
var category: BlogCategoryModel.ViewContext
}
struct Categories: Encodable {
var categoryList: BlogCategoryModel.ViewContext
}
let posts = BlogPostModel.query(on: req.db)
.sort(\.$date, .descending)
.with(\.$category)
.all()
.mapEach {Posts(posts: [=11=].viewContext, category: [=11=].category.viewContext)}
let categories = BlogCategoryModel.query(on: req.db)
.sort(\.$title)
.all()
.mapEach {Categories(categoryList: [=11=].viewContext)}
let returnContext = PageContext(posts: posts,categories: categories, title: categories)
return req.view.render("Blog/Frontend/Blog", returnContext)
}
编辑:下面是我的评论。此代码段 return 是帖子(和相关类别)。尝试向 return 添加第二个 db.query
。
func blogView(req: Request) throws -> EventLoopFuture<View> {
struct Context: Encodable {
struct PostWithCategory: Encodable {
var category: BlogCategoryModel.ViewContext
var post: BlogPostModel.ViewContext }
let title: String
let items: [PostWithCategory]
}
return BlogPostModel.query(on: req.db)
.sort(\.$date, .descending)
.with(\.$category)
.all()
.mapEach {
Context.PostWithCategory(category: [=12=].category.viewContext, post: [=12=].viewContext)
}
.flatMap {
let context = Context(title: "Blog Posts", items: [=12=])
return req.view.render("Blog/Frontend/Blog", context)
}
}
您的问题是您的 PageContext
类型具有 [Post]
和 [Categories]
类型的属性,但您正在尝试使用 EventLoopFuture<[Post]>
初始化 PageContext
和 EventLoopFuture<[Categories]>
。你可以做的是使用 NIO 的 .and
方法将 2 EventLoopFuture
对象组合成 1:
posts.and(categories) // EventLoopFuture<([Post], [Categories])>
然后您可以映射到这个新的 EventLoopFuture
以获取元组并创建您的 PageContext
实例:
return posts.and(categories).flatMap { posts, categories in
let returnContext = PageContext(posts: posts, categories: categories, title: "Blog Posts")
return req.view.render("Blog/Frontend/Blog", context)
}
我尝试过映射、平面映射、创建 Post
的 EventLoopFuture
等...我正在尝试 return 两个 db.queries
创建要在 req.view.render
上呈现的单独内容数组,其中 return 是 EventLoopFuture<View>
。
我的错误是:Cannot convert value of type 'EventLoopFuture<[Posts]>' to expected argument type '[Posts]'.
这是路由器调用的我的代码:
func blogView(req: Request) throws -> EventLoopFuture<View> {
struct PageContext: Encodable {
var posts: [Posts]
var categories: [Categories]
var title: String
}
struct Posts: Encodable {
var posts: BlogPostModel.ViewContext
var category: BlogCategoryModel.ViewContext
}
struct Categories: Encodable {
var categoryList: BlogCategoryModel.ViewContext
}
let posts = BlogPostModel.query(on: req.db)
.sort(\.$date, .descending)
.with(\.$category)
.all()
.mapEach {Posts(posts: [=11=].viewContext, category: [=11=].category.viewContext)}
let categories = BlogCategoryModel.query(on: req.db)
.sort(\.$title)
.all()
.mapEach {Categories(categoryList: [=11=].viewContext)}
let returnContext = PageContext(posts: posts,categories: categories, title: categories)
return req.view.render("Blog/Frontend/Blog", returnContext)
}
编辑:下面是我的评论。此代码段 return 是帖子(和相关类别)。尝试向 return 添加第二个 db.query
。
func blogView(req: Request) throws -> EventLoopFuture<View> {
struct Context: Encodable {
struct PostWithCategory: Encodable {
var category: BlogCategoryModel.ViewContext
var post: BlogPostModel.ViewContext }
let title: String
let items: [PostWithCategory]
}
return BlogPostModel.query(on: req.db)
.sort(\.$date, .descending)
.with(\.$category)
.all()
.mapEach {
Context.PostWithCategory(category: [=12=].category.viewContext, post: [=12=].viewContext)
}
.flatMap {
let context = Context(title: "Blog Posts", items: [=12=])
return req.view.render("Blog/Frontend/Blog", context)
}
}
您的问题是您的 PageContext
类型具有 [Post]
和 [Categories]
类型的属性,但您正在尝试使用 EventLoopFuture<[Post]>
初始化 PageContext
和 EventLoopFuture<[Categories]>
。你可以做的是使用 NIO 的 .and
方法将 2 EventLoopFuture
对象组合成 1:
posts.and(categories) // EventLoopFuture<([Post], [Categories])>
然后您可以映射到这个新的 EventLoopFuture
以获取元组并创建您的 PageContext
实例:
return posts.and(categories).flatMap { posts, categories in
let returnContext = PageContext(posts: posts, categories: categories, title: "Blog Posts")
return req.view.render("Blog/Frontend/Blog", context)
}