Maybe question, HDBC

Hello, could you please fix the code, I have tried everything. The database module is HDBC.

getUser :: PostgresConnection -> String -> String -> Maybe User
getUser conn username password = do
    result <- liftIO $ do
      let myQuery = "SELECT * FROM users WHERE username=? AND password=?;"
      select <- prepare conn myQuery
      execute select [toSql username, toSql password]
      fetchRow select

    case result of
      Just row -> do
        let user = fromSqlRowToUser row
        return user
      Nothing -> do return (Nothing::Maybe User)

The error is with the last line that is supposed to return Nothing:

    • Couldn't match type ‘Maybe User’ with ‘User’
      Expected: Maybe User
        Actual: Maybe (Maybe User)

As you are doing some IO operations, I think the return type should not be merely Maybe User. Perhaps you meant something like IO (Maybe User) or SomeMonad (Maybe User) where SomeMonad is the desired action monad?

1 Like

Thank you. Your solution resolved the question. I used the IO (Maybe User).