haskell - 如何在 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’
我怎样才能解决这个问题?
解决方案
第一个问题是它maybeAuthId
可能会失败,你必须检查一下:
maid <- maybeAuthId
case maid of
Just id_ -> selectList ...
_ -> do something in case of unaothorized user
第二个问题很简单——在 Persistent 函数中使用时,您必须对实体和字段名称进行高分:
selectList [UserIdent ==. id_] []
推荐阅读
- python - 在 Google Colab 中解压失败
- javascript - 使用 onClick 从数组中删除元素并做出反应
- python - 带有一些常见条目的 df.plot 子图的常见图例
- python - 为什么我的 Python 程序返回“ValueError:数学域错误”?
- gstreamer - 使用 GStreamer 通过 UDP 发送 GRAY8 视频时出现模糊
- pagination - API 光标分页跳转到特定页面
- angular - Angular 自定义下拉组件 - 选项更改时 ngModel 无法正确更新
- eclipse - 你能从 Eclipse 上传整个工作集到 GitHub 吗?
- api - 如何在空手道框架的场景大纲中添加“abortedStepsShouldPass”配置?
- rest - 在技术上是 RESTful 是否有两个标识符彼此之后不被 id 分隔(即 /location/addresses)