-
Notifications
You must be signed in to change notification settings - Fork 1
Open
Description
The error NameError: name 'jsonify' is not defined suggests that the jsonify function is not imported or defined in your code. To fix this issue, you need to import the jsonify function from the Flask module.
In your code, you can add the following import statement at the top of your file:
from flask import jsonifyMake sure to add this import statement before the get_users function.
Here is the updated code:
from flask import Flask, jsonify, request, logging
def get_users():
age = request.args.get('age')
if age:
try:
age = int(age) # This can raise a ValueError
except ValueError:
logging.error("ERROR: Invalid age format provided.")
return jsonify({"error": "Invalid age format"}), 400
results = []
for entry in data:
if 'age' in entry:
# Avoiding TypeError by ensuring both are integers for comparison
if age and int(entry["age"]) == age:
results.append(entry)
elif not age:
results.append(entry)
return jsonify(results)
if __name__ == "__main__":
app.run(debug=True)By importing jsonify from the Flask module, you will be able to use it in the get_users function to convert the results into a JSON response.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels