Bird
0
0

Identify the error in this code snippet:

medium📝 Debug Q6 of 15
Flask - WebSocket and Real-Time
Identify the error in this code snippet:
from flask_restx import Api, Namespace, Resource

api = Api()
ns = Namespace('orders')

@ns.route('/all')
class OrderList(Resource):
    def get(self):
        return {'orders': []}

api.add_namespace(ns)
ANamespace is not added to an Api instance
BNamespace variable is named 'ns' but decorator uses 'ns' correctly
CApi() is called without a Flask app instance
DResource class missing a constructor
Step-by-Step Solution
Solution:
  1. Step 1: Inspect Api initialization

    api = Api() creates the Api without a Flask app instance.
  2. Step 2: Consequence

    The Namespace routes are added to api, but without a Flask app and api.init_app(app), they won't function in a running server.
  3. Final Answer:

    Api() is called without a Flask app instance -> Option C
  4. Quick Check:

    Missing Flask app for Api [OK]
Quick Trick: Always link Api to Flask app [OK]
Common Mistakes:
MISTAKES
  • Creating Api without Flask app
  • Not adding Namespace to Api
  • Assuming Resource needs constructor

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes