1

我正在更改我现有的 Yesod 应用程序以在 SQL 后端而不是 mongo 上运行。生成的表结构比 mongo 后端更严格。插入时应正确创建外键引用。

postFeedingsR :: Handler RepJson
postFeedingsR  = do
  muser <- maybeAuth
  parsedFeeding <- parseJsonBody_ --get content as JSON
  let userId = getUserId muser
  let feedingWithUser = Feeding (feedingDate parsedFeeding) (feedingSide parsedFeeding) (feedingTime parsedFeeding) (feedingExcrements parsedFeeding) (feedingRemarks parsedFeeding) userId --should be linked to user..
  fid <- runDB $ insert feedingWithUser --store in database
  --runDB $ update fid [ FeedingUserId =. userId ] --Old mongo style of linking the feeding to the user
  sendResponseCreated $ FeedingR fid --return the id

我尝试使用来自 MaybeAuth 的用户 UID 更新从 parseJsonBody 获得的实体。但是,这给了我以下错误:

No instance for (aeson-0.6.0.2:Data.Aeson.Types.Class.FromJSON
                   (FeedingGeneric backend0))
  arising from a use of `parseJsonBody_'
Possible fix:
  add an instance declaration for
  (aeson-0.6.0.2:Data.Aeson.Types.Class.FromJSON
     (FeedingGeneric backend0))
In a stmt of a 'do' block: parsedFeeding <- parseJsonBody_
In the expression:
  do { muser <- maybeAuth;
       parsedFeeding <- parseJsonBody_;
       let userId = getUserId muser;
       let feedingWithUser
             = Feeding
                 (feedingDate parsedFeeding)
                 (feedingSide parsedFeeding)
                 (feedingTime parsedFeeding)
                 (feedingExcrements parsedFeeding)
                 (feedingRemarks parsedFeeding)
                 userId;
       .... }
In an equation for `postFeedingsR':
    postFeedingsR
      = do { muser <- maybeAuth;
             parsedFeeding <- parseJsonBody_;
             let userId = ...;
             .... }

我不确定为什么会这样。谁能让我找到正确的方向来解决这个问题?

4

1 回答 1

0

Solved by changing the auth line to:

Entity uid u <- requireAuth

and by adding the function:

addUserToFeeding :: UserId -> Feeding -> Feeding                                                                                                                                                            
addUserToFeeding uid Feeding {feedingDate=date, feedingSide=side, feedingTime=time, feedingExcrements=ex, feedingRemarks=remarks} = Feeding date side time ex remarks uid     

to create a new Feeding with associated user. This Feeding can then be stored in the normal way in Yesod:

let feedingWithUser = addUserToFeeding uid parsedFeeding                                                                                                                                              
fid <- runDB $ insert feedingWithUser --store in database                                
于 2012-11-17T08:55:49.203 回答