如何获取yesod中的post数据?

How to get post data in yesod?

您好,我想在 table 中手动插入数据,并且我还使用了 lookupPostParam。

这是发布新消息中的代码。

      postNewsR :: Handler Html
      postNewsR = do
                now <- liftIO getCurrentTime
                newsTitle <- lookupPostParam "title"
                newsUrl <- lookupPostParam "news_url"
                newsSnapshot <- lookupPostParam "news_snopshot"
                newsArea <- lookupPostParam "news_area"
                newsSubject <- lookupPostParam "news_subject"
                newsContent <- lookupPostParam "news_content"

               newsId <- runDB $ insert News newsTitle newsUrl newsSnapshot newsContent False Nothing now Nothing

              redirect NewsR

但它给了我以下错误:

      Couldn't match type ‘Control.Monad.Trans.Reader.ReaderT
                   (PersistEntityBackend
                      (Data.Text.Internal.Text
                       -> Data.Text.Internal.Text
                       -> Data.Text.Internal.Text
                       -> Data.Text.Internal.Text
                       -> Bool
                       -> Maybe Int
                       -> UTCTime
                       -> Maybe UTCTime
                       -> News))
                   m0
                   (Key
                      (Data.Text.Internal.Text
                       -> Data.Text.Internal.Text
                       -> Data.Text.Internal.Text
                       -> Data.Text.Internal.Text
                       -> Bool
                       -> Maybe Int
                       -> UTCTime
                       -> Maybe UTCTime
                       -> News))’
          with ‘Maybe Data.Text.Internal.Text
                -> Maybe Data.Text.Internal.Text
                -> Maybe Data.Text.Internal.Text
                -> Maybe Data.Text.Internal.Text
                -> Bool
                -> Maybe a0
                -> UTCTime
                -> Maybe a1
                -> Control.Monad.Trans.Reader.ReaderT
                     (YesodPersistBackend App) (HandlerT App IO) t0’
         Expected type: Maybe Data.Text.Internal.Text
           -> Maybe Data.Text.Internal.Text
           -> Maybe Data.Text.Internal.Text
           -> Maybe Data.Text.Internal.Text
           -> Bool
           -> Maybe a0
           -> UTCTime
           -> Maybe a1
           -> YesodDB App t0
         Actual type: Control.Monad.Trans.Reader.ReaderT
             (PersistEntityBackend
                (Data.Text.Internal.Text
                 -> Data.Text.Internal.Text
                 -> Data.Text.Internal.Text
                 -> Data.Text.Internal.Text
                 -> Bool
                 -> Maybe Int
                 -> UTCTime
                 -> Maybe UTCTime
                 -> News))
             m0
             (Key
                (Data.Text.Internal.Text
                 -> Data.Text.Internal.Text
                 -> Data.Text.Internal.Text
                 -> Data.Text.Internal.Text
                 -> Bool
                 -> Maybe Int
                 -> UTCTime
                 -> Maybe UTCTime
                 -> News))
      The function insert is applied to 9 arguments,
      but its type ‘(Data.Text.Internal.Text
           -> Data.Text.Internal.Text
           -> Data.Text.Internal.Text
           -> Data.Text.Internal.Text
           -> Bool
           -> Maybe Int
           -> UTCTime
           -> Maybe UTCTime
           -> News)
          -> Control.Monad.Trans.Reader.ReaderT
               (PersistEntityBackend
                  (Data.Text.Internal.Text
                   -> Data.Text.Internal.Text
                   -> Data.Text.Internal.Text
                   -> Data.Text.Internal.Text
                   -> Bool
                   -> Maybe Int
                   -> UTCTime
                   -> Maybe UTCTime
                   -> News))
               m0
               (Key
                  (Data.Text.Internal.Text
                   -> Data.Text.Internal.Text
                   -> Data.Text.Internal.Text
                   -> Data.Text.Internal.Text
                   -> Bool
                   -> Maybe Int
                   -> UTCTime
                   -> Maybe UTCTime
                   -> News))’

希望你能帮助我,在此先感谢

编辑:


我更改了我的代码中的一些内容。这是更新。 从 lookupPostParam 到 runInputPost

    newsTitle <- runInputPost $ ireq textField "title"
    newsUrl <- runInputPost $ ireq textField "news_url"
    newsSnapshot <- runInputPost $ ireq textField "news_snopshot"
    newsArea <- runInputPost $ ireq textField "news_snopshot"
    newsSubject <- runInputPost $ ireq textField "news_snopshot"
    newsContent <- runInputPost $ ireq textareaField "news_content"

    -- Inserting it to the table News
    newsId <- insert $ News newsTitle newsUrl newsSnapshot newsContent False Nothing now Nothing

现在它给了我 2 个错误:

 1. Couldn't match type ‘Control.Monad.Trans.Reader.ReaderT
                   SqlBackend m0’
          with ‘HandlerT App IO’
    Expected type: HandlerT App IO (Key News)
    Actual type: Control.Monad.Trans.Reader.ReaderT
             SqlBackend m0 (Key News) …

提前致谢。

了解您如何定义 News 实体会很有帮助...

让我们看一下相关函数的签名:

lookupPostParam :: (MonadResource m, MonadHandler m)
                => Text
                -> m (Maybe Text)

也就是说 lookupPostParam 有一个 Text 参数,return 是一个 Maybe Text,并且存在于 m monad 中。当您调用 lookupPostParam 时,<- 关键字会为我们解包 m monad。所以 newsTitle

newsTitle <- lookupPostParam "title"

具有类型 Maybe Text。我想你的 News 将其字段定义为 Text 类型,所以你仍然必须处理 Maybe 才能获得可以插入数据库的值。例如,

case newsTitle of
 Nothing -> redirect NewsR -- and handle the error, return 400 perhaps
 Just title -> do
   newsUrl <- lookupPostParam "news_url"
   case newsUrl of
    Nothing -> redirect NewsR -- and handle the error
    Just url -> do
      -- and so on

lookupPostParam 的 return 值解包 Maybe 后,将它们应用到 insert.

应该没有问题
newsId <- runDB $ insert $ 
          News title url snapshot content False Nothing now Nothing

看看 Data.Maybe 或在此处回复,如果您仍然卡住。