在 Vapor 命令中使用 Fluent

问题描述

我正在尝试在命令中使用 Fluent,但我遇到了这个问题:

关于这个查询

let location: Location? = try? Location.query(on: context.application.db).filter(\.geId == 123).first().wait()

我得到: Type of expression is ambiguous without more context"


当我移除过滤器时,一切都很好:

let location: Location? = try? Location.query(on: context.application.db).first().wait()

当我将 var 设为非可选时:

let location: Location = try? Location.query(on: context.application.db).filter(\.geId == 123).first().wait()

我得到: Cannot convert value of type 'Location?' to specified type 'Location'

相同的查询正在使用 flatMap 而不是 wait()

处理 Route

所以,我有点困惑。我的错误在哪里?

解决方法

这是一个简单的... 只是错过了

import Fluent

感谢 Discord 的@0xTim :)