列表中的每个元素都会触发一些数据库更新,但我一直卡在返回类型上。你能告诉我这样做的正确方法是什么吗?
for :: [a] -> (a -> b) -> [b]
for xs f = map f xs
-- Summary: Loop over the elements of xs and update the table for each element
-- get an ID from the element
-- get the record corresponding to that ID
-- extract more values
-- update table (below is just a dummy updateWhere
-- but the above values will be used in the update eventually )
forM_ xs $ \(Entity xid val) -> do
let qid = tableField val
y <- runDB $ get404 qid
let z = table2Field y
return $ updateWhere [PersonName ==. "Test"] [PersonAge *=. 1]
我收到以下错误:
Couldn't match type `PersistMonadBackend m0'
with `persistent-1.2.1:Database.Persist.Sql.Types.SqlBackend'
Expected type: PersistMonadBackend m0
Actual type: PersistEntityBackend Person
In the second argument of `($)', namely
`updateWhere [PersonName ==. name] [PersonAge *=. 1]'
我尝试使用 for
而不是 forM or forM_
它并没有解决任何问题。在这一点上,我只是在尝试一大堆组合,而没有真正理解如何解决这个错误。感谢你的帮助!
更新:
这是我正在使用的实际代码。当我摆脱大多数 let 语句并只运行 updateWhere 并进行微不足道的更新时,它仍然给我同样的错误。
getCalculateDeltaR :: personId -> Handler Html
getCalculateDeltaR personId = do
goods <- runDB $ selectList [GoodPerson ==. personId] []
forM goods $ \(Entity gid good) -> do
let aid = goodAsset good
asset <- runDB $ get404 aid
let mktValue = assetMktValue asset
return $ updateWhere [GoodPerson ==. personId, GoodAsset = aid] [GoodDelta =. (mktValue - GoodOrigValue)]
defaultLayout $ do
$(widgetFile "calculateDelta")
如果我将上面的表格更改为:
forM goods $ \(Entity gid good) -> do
return $ updateWhere [GoodPerson ==. personId] [GoodDelta =. 1]
关于不匹配的类型,我仍然遇到同样的错误。