Flask和MongoDB(mLab)TypeError:ObjectId('5bf0b6ce397b3635dc0b10ab')不可序列化JSON

时间:2018-11-18 01:04:24

标签: python mongodb flask mlab

我正在构建一个基于Flask的Web应用程序,并且在我的数据库帮助器类中,我有一个create_user()函数,如果该行的内容如下,则该函数可以正常工作:

users.insert({'user': email, 'password': hashpass})

但是,当我将其更改为:

users.insert({'email': email, 'password': hashpass})

我收到以下错误:

TypeError: ObjectId('5bf0b6ce397b3635dc0b10ab') is not JSON serializable

数据库帮助程序类

import bcrypt

class DBHelper:

def __init__(self, mongo):
    self.mongo = mongo

def create_user(self, email, password):
    users = self.mongo.db.users
    hashpass = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())
    users.insert({'email': email, 'password': hashpass})

烧瓶代码

@app.route("/account")
@login_required
def account():
    return render_template("home.html", options=options, questions=questions)
    return "You are logged in"


@app.route('/register', methods=['POST', 'GET'])
def register():
    if request.method == 'POST':
        stored_user = DB.get_user(request.form['email'])

        if stored_user is None:
            DB.create_user(request.form['email'], request.form['pass'])
            #session['username'] = request.form['username']
            user = User(DB.get_user(request.form['email']))
            login_user(user)
            return redirect(url_for('account'))

        return 'That username already exists!'

return render_template('register.html')

1 个答案:

答案 0 :(得分:0)

问题出在以下代码上:

user = User(DB.get_user(request.form['email']))
login_user(user)

我正在将字典传递给User类,而不是电子邮件地址。