当使用Flask 0.10.1时,得到'TypeError:ObjectId('')不是JSON序列化的' [英] Getting 'TypeError: ObjectId('') is not JSON serializable' when using Flask 0.10.1

查看:2052
本文介绍了当使用Flask 0.10.1时,得到'TypeError:ObjectId('')不是JSON序列化的'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我把Flask的例子Minitwit分支到MongoDB上工作,它在Flask 0.9上工作正常,但升级到0.10.1后,当我尝试设置会话ID时,登录名时出现错误。 / p>

似乎有

代码片段:

  user = db.minitwit.user.find_one({'username':request.form ['username']})
session ['_ id'] = user [ _id']

完整的代码在我的 github repo。



基本上,我设置了Flask会话ID从MongoDB到用户的_id。



我试着从这个 so question 没有成功。



那么,session ['_ id'] = str(user ['_ id'])摆脱了错误消息,我正确地重定向到时间线页面,但我没有实际登录。



我该如何解决这个问题? b
$ b

编辑:复制/追踪追踪: http://pastebin.com/qa0AL1fk



谢谢。

解决方案

更容易修复。你甚至不需要做任何JSON编码/解码。



只需将会话['_ id']保存为字符串:

  user = db.minitwit.user.find_one({'username':request.form ['username']})
session ['_ id' ] = str(user ['_ id'])

然后到处都想用session ['_ id']你必须用ObjectId()包装它,所以它作为一个ObjectId对象传递给MongoDB。

  if会话中的'_id':
g.user = db.minitwit.user.find_one({'_ id':session ['_ id']})

$ p
$ b $ pre $ if $'$ id
g.user = db.minitwit.user.find_one({'_ id':ObjectId(session ['_ id']))

您可以在我的 github repo 上看到完整的差异。

如果一个nyone关心的是为什么在Flask 0.10.1中出现TypeError:ObjectId('')不是JSON序列化的问题,这是因为他们改变了会话存储的方式。他们现在被存储为JSON,因为MongoDB中的'_id'对象不是标准的JSON,它没有序列化会话令牌,从而给出TypeError。阅读这里的变化: http://flask.pocoo.org/docs/upgrading/#upgrading- to-010


I forked the Flask example, Minitwit, to work with MongoDB and it was working fine on Flask 0.9, but after upgrading to 0.10.1 I get the error in title when I login when I try to set the session id.

It seems there was changes in Flask 0.10.1 related to json.

Code snippet:

user = db.minitwit.user.find_one({'username': request.form['username']})
session['_id'] = user['_id']

Full code in my github repo.

Basically, I set the Flask session id to the user's _id from MongoDB.

I tried the first two solution from this SO question without success.

Well, doing session['_id'] = str(user['_id']) gets rid of the error message and I'm properly redirected to the timeline page but I am not actually logged in.

How can I fix this?

EDIT: Copy/paste of the traceback: http://pastebin.com/qa0AL1fk

Thank you.

解决方案

EDIT: Even easier fix. You don't even need to do any JSON encoding/decoding.

Just save the session['_id'] as a string:

user = db.minitwit.user.find_one({'username': request.form['username']})
session['_id'] = str(user['_id'])

And then everywhere you want to do something with the session['_id'] you have to wrap it with ObjectId() so it's passed as a ObjectId object to MongoDB.

if '_id' in session:
    g.user = db.minitwit.user.find_one({'_id': session['_id']})

to:

if '_id' in session:
    g.user = db.minitwit.user.find_one({'_id': ObjectId(session['_id'])})

You can see the full diff for the fix on my github repo.

If anyone cares to know why the 'TypeError: ObjectId('') is not JSON serializable' "issue" appeared in Flask 0.10.1, it's because they changed the way sessions are stored. They are now stored as JSON so since the '_id' object in MongoDB isn't standard JSON, it failed to serialize the session token, thus giving the TypeError. Read about the change here: http://flask.pocoo.org/docs/upgrading/#upgrading-to-010

这篇关于当使用Flask 0.10.1时,得到'TypeError:ObjectId('')不是JSON序列化的'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆