如何从处理程序访问全局MongoDB连接?
我正在尝试使用Snap制作一个简单的web应用程序。我希望有一个全局MongoDB连接,可以跨处理程序使用 我在如何从处理程序访问全局MongoDB连接?,mongodb,haskell,state,haskell-snap-framework,Mongodb,Haskell,State,Haskell Snap Framework,我正在尝试使用Snap制作一个简单的web应用程序。我希望有一个全局MongoDB连接,可以跨处理程序使用 我在appInit中打开此连接,它可以正常工作 data App = App { _mongoDB :: Pipe -- connection } appInit :: SnapletInit App App appInit = makeSnaplet "site" "My Site" Nothing $ do db <- liftIO $ d
appInit
中打开此连接,它可以正常工作
data App = App { _mongoDB :: Pipe -- connection
}
appInit :: SnapletInit App App
appInit = makeSnaplet "site" "My Site" Nothing $ do
db <- liftIO $ do
pipe <- runIOE $ connect (host "127.0.0.1")
return pipe
return $ App db
它给了我以下错误:
No instance for (MonadState App IO)
arising from a use of `gets'
Possible fix: add an instance declaration for (MonadState App IO)
In a stmt of a 'do' block: pipe <- gets _mongoDB
In the second argument of `($)', namely
`do { pipe <- gets _mongoDB;
results <- access pipe master "db" (find $ select [] "movies");
return results }'
In a stmt of a 'do' block:
res <- liftIO
$ do { pipe <- gets _mongoDB;
results <- access pipe master "db" (find $ select [] "movies");
return results }
没有(MonadState应用程序IO)的实例
由使用“get”引起的
可能的修复方法:为(MonadState应用程序IO)添加实例声明
在'do'块的stmt:pipe中,liftIO
块仅用于IO操作,需要在处理程序
monad本身中访问应用程序状态
watchHandler :: Handler App App ()
watchHandler = do
pipe <- gets _mongoDB
res <- liftIO $ do
results <- access pipe master "db" (find $ select [] "movies")
return results
writeBS "test"
watchHandler::Handler应用程序()
watchHandler=do
管
watchHandler :: Handler App App ()
watchHandler = do
pipe <- gets _mongoDB
res <- liftIO $ do
results <- access pipe master "db" (find $ select [] "movies")
return results
writeBS "test"
watchHandler :: Handler App App ()
watchHandler = do
pipe <- gets _mongoDB
res <- liftIO $ access pipe master "db" (find $ select [] "movies")
writeBS "test"