Hi,
I’m trying to model an optional Parent/Child relationship.
I have 2 models: an User and a Location.
Every User may belong to a Location:
// User Model
final class User: Codable {
var id: UUID?
var name: String
var username: String
var locationID: Location.ID?
init(name: String, username: String, locationID: Location.ID? = nil) {
self.name = name
self.username = username
self.locationID = locationID
}
}
extension User {
var location: Parent<User, Location>? {
return parent(\.locationID)
}
}
// Location Model
final class Location: Codable {
var id: Int?
var short: String
var description: String
init(short: String, description: String) {
self.short = short
self.description = description
}
}
As you can see Usre’s locationID property is Optional<Location.ID> because a User may not have a Location.
In UsersController I have a getLocationHandler(req:) to extract Location, if any, for a particular User
func getLocationHandler(_ req: Request) throws -> Future<Location?> {
return try req
.parameters.next(User.self)
.flatMap(to: Location.self) {
user in
user.location?.get(on: req)
}
}
The code doesn’t compile on this line:
user.location?.get(on: req) // Expression type `_?` is ambiguous without more context
The message is quite cryptic to me: I think I have to explicit some types to make the things clear to the compiler, but I can’t figure out what.
Could you please help me?
Thank you, Luca