首页 > 解决方案 > 如何在 Yesod 持久化中从 Sqllite 中提取值

问题描述

我第一次使用 Yesod,我尝试了脚手架网站,通过一些小的更改我能够实现 googleOauth,我从 google 取回 uid 和一些用户信息,然后将它们保存到 sqlite 数据库。这里是日志那个authenticate用来验证的,仅供参考

SELECT "id","ident","email","name","picture" FROM "user" WHERE "ident"=?; [PersistText "google-uid:11111111111"]

我的用户模型定义如下

User
    ident Text
    email Text
    name Text
    picture Text
    UniqueUser ident
    deriving Typeable

在随后的处理中,我想使用从谷歌收到的 id 查询数据库,以提取电子邮件、姓名和图片。我试着写这样的东西

maid <- maybeAuthId
    let user = selectList [userIdent ==. maid] []

但它给了我以下错误

Couldn't match expected type ‘EntityField record (Maybe UserId)’
                      with actual type ‘User -> Text’

我怎样才能解决这个问题?

标签: haskellyesod

解决方案


第一个问题是它maybeAuthId可能会失败,你必须检查一下:

maid <- maybeAuthId
case maid of
  Just id_ -> selectList ...
  _ -> do something in case of unaothorized user

第二个问题很简单——在 Persistent 函数中使用时,您必须对实体和字段名称进行高分:

selectList [UserIdent ==. id_] []

推荐阅读