Summary: I am pulling a list of user IDs from a members transaction table that is then used to generate a list of users from the database. This is then used to generate JSON sent to a page that lists users associated with an account.
Background: Scala code using Squeryl on Play! Framework
The Error: value id is not a member of Option[models.User] when mapping the users to the JSON generator
The Code: Here’s code being used in succession to finally generate the JSON
def memberIds(accountId: Long) = { from(DB.members)(m =>
where(m.accountId === accountId)
select (m)).map(_.userId).toSet
}
def membersToAccount(id: Long) = {
memberIds(id).map( i => models.User.get(i))
}
def listMembers(accountId: Long) = {
Json(generate(Account.membersToAccount(accountId)
.map(user => Map(
"id" -> user.id,
"name" -> user.name,
"username" -> user.username
))))
}
The Confusion: the User class contains an id val that looks like case class User( I’m confused as to why it would create this error. Is it a problem with syntax or where I am mapping the users? Thanks!
val id: Long, etc.
That’s cause models.User is wrapped in Option container (actually, that is monad). It’s like trying to call method of User directly on List of users, like:
List(someUsers).idBasically that means, that this value can be either Some (non null wrapper) or Nothing (just like
nullbut better).Moreover, it is better not to use
geton Options, consider link above for details.