0

我正在使用注册系统,提交时出现错误。不确定我是否如何提交我的信息,但它在邮递员上工作正常。

这是错误。

    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:\users\ryand\.virtualenvs\main-api-ucgvpon1\lib\site-packages\flask\app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "c:\users\ryand\.virtualenvs\main-api-ucgvpon1\lib\site-packages\flask\app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "c:\users\ryand\.virtualenvs\main-api-ucgvpon1\lib\site-packages\flask_cors\extension.py", line 165, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "c:\users\ryand\.virtualenvs\main-api-ucgvpon1\lib\site-packages\flask\app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "c:\users\ryand\.virtualenvs\main-api-ucgvpon1\lib\site-packages\flask\_compat.py", line 39, in reraise
    raise value
  File "c:\users\ryand\.virtualenvs\main-api-ucgvpon1\lib\site-packages\flask\app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "c:\users\ryand\.virtualenvs\main-api-ucgvpon1\lib\site-packages\flask\app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:\Users\ryand\Desktop\mealplansfree\main-api\api.py", line 98, in decorated
    return f(application, *args, **kwargs)
  File "C:\Users\ryand\Desktop\mealplansfree\main-api\api.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)
    })
}
4

1 回答 1

0

刚刚为我解决的问题是在我的 axios 帖子中添加以下标题。

'Content-Type': 'application/json'

于 2020-11-02T04:14:00.583 回答