The problem is that GHCi runs your code in the IO
monad, while your function is in the HandlerT App IO
monad. But HandlerT
is a monad transformer over the IO
monad, so you can use lift
to "promote" your IO
action to a HandlerT App IO
action. The type of lift is:
lift :: (MonadTrans t, Monad m) => m a -> t m a
In your case, t
has to be HandlerT App
and m
has to be IO
.
So the correct code looks like this:
getActivityR :: Handler Import.Value
getActivityR = do
pipe <- lift $ runIOE $ connect $ Host "XXXXX.mongolab.com" $ PortNumber 33317
access pipe master <databaseName> $ auth <username> <password>
access pipe master <databaseName> allCollections
returnJson $ object ["activity" .= ("abc" :: Text)]
I don't know what your access
is, so I can't tell whether you need the lift there too or not.
For your special case, there is also liftIO
which is like lift
, but lifts straight to IO, not only to the next "layer" in your stack of monad transformers, so you should use it:
pipe <- liftIO $ runIOE $ connect $ Host "XXXXX.mongolab.com" $ PortNumber 33317
If you want to know more about this topic you should look for "monad transformers."