Bird
0
0

What is wrong with this code?

medium📝 Debug Q7 of 15
Flask - WebSocket and Real-Time
What is wrong with this code?
from flask_restx import Api, Namespace, Resource

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

@ns.route('/info')
class CustomerInfo(Resource):
    def get(self):
        return {'customer': 'info'}

api.add_namespace(ns)
ANamespace variable is 'ns' but decorator uses '@ns.route' which is undefined
BNamespace 'ns' is not linked to any Flask app
CNamespace is not added to the Api instance
DResource class missing a constructor
Step-by-Step Solution
Solution:
  1. Step 1: Check Api and app linkage

    api = Api() has no Flask app, so Namespace routes can't be linked to a server.
  2. Step 2: Verify other options

    'ns' is correctly defined and used; Namespace is added to api.
  3. Final Answer:

    Namespace 'ns' is not linked to any Flask app -> Option B
  4. Quick Check:

    Missing Flask app linkage [OK]
Quick Trick: Link Api to Flask app using constructor or init_app [OK]
Common Mistakes:
MISTAKES
  • Not linking Api to Flask app
  • Mismatched variable names
  • Forgetting to add Namespace to Api

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes