Hook函数里不能获取到engine.current_user

详细描述如下:


Hook的定义:

@engine.before_save('Vote') 
def before_vote_save(vote):
    vote.set('user',engine.current_user) #设置用户
    print "hook vote"
    print engine.current_user
    return

本地调试,模拟请求成功,可以获取current_user

$ avoscloud
提示:您可以敲入 rs 命令并回车来重启本进程
提示:您可以使用 http://localhost:3001 测试 Cloud 函数
hook vote
<leancloud.user.User object at 0x7fc134c43150>
127.0.0.1 - - [29/Dec/2015 12:48:31] "POST /1.1/functions/Vote/beforeSave HTTP/1.1" 200 325

然后deploy和publish到云引擎,再次在javascript客户端保存Vote对象,后台查看user对象为null:

{
  "clique": {
    "__type": "Pointer",
    "className": "Clique",
    "objectId": "568133e200b0cff56c5125ed"
  },
  "user": null,
  "ACL": {
    "*": {
      "read": true,
      "write": true
    }
  },
  "objectId": "568213fb00b0f9a1f223a4ab",
  "createdAt": "2015-12-29T13:02:51.196Z",
  "updatedAt": "2015-12-29T13:02:51.196Z"
}

日志输出:

INFO 2015-12-29 13:01:59 发布到生产环境……
INFO 2015-12-29 13:01:59 从测试环境拷贝代码包
INFO 2015-12-29 13:01:59 运行环境: Python
INFO 2015-12-29 13:02:06 项目代码分发
INFO 2015-12-29 13:02:16 创建新的容器
INFO 2015-12-29 13:02:18 应用启动检测
INFO 2015-12-29 13:02:19 注册云代码方法
INFO 2015-12-29 13:02:19 切换路由
INFO 2015-12-29 13:02:19 移除之前运行的容器
INFO 2015-12-29 13:02:19 更新应用元数据
INFO 2015-12-29 13:02:19 部署生产环境成功, commit: 'local:9727b6900d7d4200fe6c37bc42dadf78', log: 'Uploaded at 2015-12-29 13:00:31'
INFO 2015-12-29 13:02:51 hook vote
INFO 2015-12-29 13:02:51 None

可以看到print engine.current_user输出为None。所以问题在于Hook函数不能获取到engine.current_user,请问这是BUG吗?

我很确定用户是已登陆的,客户端新建一个字段user2,可以在客户端设置当前已登陆的user:

vote.set('user2',AV.User.current())

后台数据:

 {
  "clique": {
    "__type": "Pointer",
    "className": "Clique",
    "objectId": "568133e200b0cff56c5125ed"
  },
  "user": null,
  "user2": {
    "__type": "Pointer",
    "className": "_User",
    "objectId": "563b7b8c60b259ca7b2aa462"
  },
  "ACL": {
    "*": {
      "read": true,
      "write": true
    }
  },
  "objectId": "568218f860b2e57b8d5a6ec6",
  "createdAt": "2015-12-29T13:24:08.826Z",
  "updatedAt": "2015-12-29T13:24:08.826Z"
}

user字段为null是因为云引擎Hook设置了vote.set('user',engine.current_user),而云引擎Hook获得的engine.current_user为None。

您好,我去确认一下这个问题。

是的,正在修复,十分抱歉。

好的,辛苦了: )
密切关注中。

已经发布 1.3.12 ,更新到这个版本应该就可以解决,有问题欢迎随时反馈。