我正在用Yesod开发我的第一个应用程序,我正在创建一些CRUD api来启动。
我有个模特看起来
User json
...
Activity json
userId UserId
...其中userId是一个外键。
我需要创建一个端点,以便能够创建一个新的活动,而客户机需要能够指定userId。
要做到这一点,我使用的形式如下
postCreateActivityR :: Hadler Value
postCreateActivityR = do
activity <- runInputPost $ Activity
<$> ...
<*> ireq textField "userId"
...这样做会得到一个错误,如下所示:
Couldn't match type ‘Text’ with ‘Key User’ expected type: FormInput (HandlerT App IO) (Key User)有什么标准的办法解决这个问题吗?
发布于 2017-09-06 07:14:12
为了记录,这就是我最后解决它的方法
我不得不创造一个新的领域
userIdField :: (Monad m, RenderMessage (HandlerSite m) FormMessage) => Field m UserId
userIdField = Field
{ fieldParse = parseHelper $ \s ->
case signed decimal s of
Right (a, "") -> Right $ toSqlKey a
_ -> Left $ MsgInvalidInteger s
, fieldView = \_ _ _ _ _ -> ""
, fieldEnctype = UrlEncoded
}然后把它当作
<*> ireq userIdField "userId"发布于 2017-09-04 13:58:39
如果您使用的是SQL后端,则在toSqlKey模块中有Database.Persist.Sql。由于您获得了Text,所以首先需要使用Data.Text.Read将其转换为Int64。
https://stackoverflow.com/questions/46037708
复制相似问题