개체 배열을 사용하여 json을 받아 들일 수있는 yesod 처리기가 있습니다. 모든 개체를 데이터베이스에 삽입하고 싶습니다.나는 어떻게 aeson 파싱 된 엔티티 배열의 타입을 인식 할 수 있도록 yesod-persistent를 얻습니까?
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
더 좋네요! do 블록 안에서 패턴 매치를 할 수 있는지 몰랐다. – nont
그것은 실제로 람다에 당황하지 않습니다. '실패'덕분에 그보다 더 복잡합니다. – Carl