我有一个 yesod 处理程序,它可以接受一些带有对象数组的 json。我想将所有对象插入数据库。
newtype NodeList = NodeList [Node]
instance FromJSON NodeList where
parseJSON (Object o) = NodeList <$> o .: "nodes"
parseJSON _ = mzero
postMoreNodesR :: Handler ()
postMoreNodesR = do
nodes::NodeList <- requireJsonBody
runDB $ mapM_ insert nodes
return ()
但是有些方法,它无法识别我的实体类型。(尽管同一模块中的其他 POST 和 GET 处理程序工作得很好。)我可以说我已经很接近了,但我不确定该怎么做,因为“a0”不是我在任何地方声明的类型。这是错误:
Handler/Node.hs:46:30:
Couldn't match expected type `[a0]' with actual type `NodeList'
In the second argument of `mapM_', namely `nodes'
In the second argument of `($)', namely `mapM_ insert nodes'
In a stmt of a 'do' block: runDB $ mapM_ insert nodes