Python Flask TypeError:"NoneType"对象不可下标



我在注册系统上工作,提交时收到错误。不确定我是不是这样提交信息的,但它对邮递员来说很好。

这是错误。

127.0.0.1 - - [01/Nov/2020 17:42:40] "OPTIONS /register HTTP/1.1" 200 -
[2020-11-01 17:42:40,542] ERROR in app: Exception on /register [POST]
Traceback (most recent call last):
File "c:usersryand.virtualenvsmain-api-ucgvpon1libsite-packagesflaskapp.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "c:usersryand.virtualenvsmain-api-ucgvpon1libsite-packagesflaskapp.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "c:usersryand.virtualenvsmain-api-ucgvpon1libsite-packagesflask_corsextension.py", line 165, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "c:usersryand.virtualenvsmain-api-ucgvpon1libsite-packagesflaskapp.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "c:usersryand.virtualenvsmain-api-ucgvpon1libsite-packagesflask_compat.py", line 39, in reraise
raise value
File "c:usersryand.virtualenvsmain-api-ucgvpon1libsite-packagesflaskapp.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "c:usersryand.virtualenvsmain-api-ucgvpon1libsite-packagesflaskapp.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:UsersryandDesktopmealplansfreemain-apiapi.py", line 98, in decorated
return f(application, *args, **kwargs)
File "C:UsersryandDesktopmealplansfreemain-apiapi.py", line 148, in create_user
hashed_password = generate_password_hash(data['password'], method='sha256')
TypeError: 'NoneType' object is not subscriptable
127.0.0.1 - - [01/Nov/2020 17:42:40] "POST /register HTTP/1.1" 500 -

这是寄存器和所需的应用程序。

def application_required(f):
@wraps(f)
def decorated(*args, **kwargs):
token = None
if 'x-access-key' in request.headers:
token = request.headers['x-access-key']
if not token:
return jsonify({'message' : 'ERROR x-access-key missing or incorrect.'}), 401
if token == app.config['SECRET_KEY']:
application = "confirmed application"
else:
return jsonify({'message' : 'ERROR x-access-key missing or incorrect.'}), 401
return f(application, *args, **kwargs)
return decorated
@app.route('/register', methods=['POST'])
@application_required
def create_user(application):
data = request.get_json()
hashed_password = generate_password_hash(data['password'], method='sha256')
new_user = User(user_id=str(uuid.uuid4()), full_name=data['full_name'], username=data['username'], password=hashed_password, admin=False, birthdate=data['birthdate'], email=data['email'], country=data['country'], state_province=data['state_province'], city=data['city'])
db.session.add(new_user)
db.session.commit()
return 'User reggistered!'

下面是使用Axios提交表单的React。

buildForm() {
let formData = JSON.stringify({'full_name' : this.state.full_name, 'birthdate' : this.state.birthdate, 'username' : this.state.username, 'password' : this.state.password, 'email' : this.state.email, 'country' : this.state.country, 'state_province' : this.state.state_province, 'city' : this.state.city})
return formData;
}

handleSubmit(event) {
Axios({
method: 'post',
headers: {"x-access-key": "Token Removed ;)"},
url: 'http://127.0.0.1:5000/register',
data: this.buildForm()
}).then(response => {
console.log(response)
console.log(response.data)
}).catch(error => {
console.log(error)
})
}

刚刚为我修复的是在我的axios帖子中添加以下标题。

'Content-Type': 'application/json'

最新更新