TypeError:“ ResultProxy”对象在尝试将用户ID存储在会话中时不支持索引

时间:2018-07-18 00:21:46

标签: python-3.x session flask flask-sqlalchemy

在我的plt.show() 路由中,我试图在会话中存储用户ID并自动登录用户,但是当我尝试这样做时,出现此错误:

Signedup

而且,在我的TypeError: object of type 'ResultProxy' does not support indexing路线中,我试图记住哪个用户已登录,但在这里出现错误:

signin

TypeError: object of type 'ResultProxy' has no len()中的

signedup路线

application.py
@app.route("/signedup",methods=["GET","POST"]) def signedup(): if request.method=="POST": password = request.form.get("password") hash = pwd_context.encrypt(password) name = request.form.get("name") if not name: return "must provide username" elif not password: return "must provide password" registration = db.execute("INSERT INTO users (username,password) VALUES (:username,:password)", {"username":name,"password":hash}) if not registration: return "pick a different username" # Store their ID in session and log them in automatically user_id = db.execute("SELECT user_id FROM users where username = :username",{"username":name}) # It means something like - "give me the first row in result # and retrieve the value of the key "id" #ERROR session["user_id"] = user_id[0]["id"] db.commit() return render_template("success.html",name = name) else: return render_template("signup.html") 中的

signin路线

application.py

2 个答案:

答案 0 :(得分:0)

您可以简单地遍历resultProxy对象。

for iter in user_id:
   session["user_id"] = iter[0]

答案 1 :(得分:0)

我遇到了相同的错误,并通过在db.execute命令末尾添加.fetchone()来解决:

val splitFlexType = udf((typepath: String) =>  typepath.split("/").last)

您必须使用.fetchone(返回单个结果)或.fetchall(返回所有结果),才能对SQLAlchemy命令返回的resultProxy对象使用索引。

相关问题