我遇到了持久性匹配类型的问题。我有一个名为Storage.Mongo的模块,如下所示:
type FieldMap = Map.Map T.Text T.Text
let mongoSettings = (mkPersistSettings (ConT ''MongoBackend)) {mpsGeneric = False}
in share [mkPersist mongoSettings] [persistLowerCase|
Notice
rawData FieldMap
deriving Show
|]
-- | This is the default database pool
defaultPool = createMongoDBPool
"system_of_record"
"localhost"
(PortNumber 33107)
(Just (MongoAuth "reader" "password"))
10
10
30
-- | Save notice to database
saveNotices x = do pool <- defaultPool
runMongoDBPoolDef save pool
where save = mapM_ (insert.Notice) x
我正在尝试传递saveNotices命令一个字段映射,它将转换为Notice实体并保存到MongoDb数据库。即:
main = do files <- getArgs
mapM_ parseNotice files
where parseNotice f = do x <- parseFromFile fboFile f
case x of
Left err -> print err
Right notices -> mapM_ saveNotices notices
parseNotice函数返回一个Maps列表:
notice = do noticeType <- openingTag
fields <- manyTill (try complexField <|> simpleField) (try closingTag)
return $ (Map.fromList(concat ([("NOTICETYPE", noticeType)]:fields)))
fboFile = many notice
我不确定问题出在哪里。我相信我应该让编译器知道
的类型mapM_ (insert.Notice) x
是,但我不确定应该是什么类型
这是我得到的错误
无法将类型'PersistEntityBackend Notice'与'MongoBackend'匹配预期类型:PersistEntityBackend注意事项实际类型:PersistMonadBackend(Action m)在'(。)'的第一个参数中,即'insert'在'的第一个参数中' mapM_',即'(插入。通知)'在表达式中:mapM_(插入。通知)x
答案 0 :(得分:1)
未能尝试应用insert . Notice
(为强调添加空间)
insert :: MonadIO' m => Collection -> Document -> Action m Value
type Collection = Text
看起来insert
期望名称作为其第一个参数。以下可能有效。
mapM_ (insert "Notice" $ Notice) x