Hello I want to manually insert a data in the table and also i used the lookupPostParam.
here's the code in Posting a New News.
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
but it gives me this Following error:
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))’
I hope you help me, Thanks in advance
EDIT:
I change something in my code. here's the update. From lookupPostParam to 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
Now it gives me 2 error:
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) …
Thanks in advance.
It would be helpful to see how you've defined your News
entity...
Let's take a look at the signature of the function in question:
lookupPostParam :: (MonadResource m, MonadHandler m)
=> Text
-> m (Maybe Text)
That says lookupPostParam
takes a Text
argument, returns a Maybe Text
, and lives in the m
monad. When you invoke lookupPostParam
, the <-
keyword unwraps the m
monad for us. So newsTitle
in
newsTitle <- lookupPostParam "title"
has the type Maybe Text
. I would imagine your News
defines its fields as Text
types, so you still have to handle the Maybe
before you will have values you can insert in your database. E.g.,
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
Once you've unwrapped Maybe
from the return values of lookupPostParam
, you should have no trouble applying them to insert
.
newsId <- runDB $ insert $
News title url snapshot content False Nothing now Nothing
Take a look at Data.Maybe or reply back here if you're still stuck.