I have a vapor (4) server and recently added a function to query the database for a resident using an email address:
func getResidentByEmail(req: Request) throws -> EventLoopFuture<Resident> {
let token = try req.auth.require(Token.self)
let email = req.parameters.get("email") ?? ""
return Resident.query(on: req.db)
.filter(.$email == email)
.first()
.map { resident in
guard let resident else { return Resident() }
return resident
}
}
Initially this function worked: I was able to return a resident with an email address. Now my server crashes with this error:
FluentKit/Field.swift:23: Fatal error: Cannot access field before it is initialized or fetched: address
I don’t understand where I’m accessing the address field before it’s initialized. If I restart the server this might say phoneNumber instead of address or some other field, but the rest of the error stays the same.