Bird
0
0

Identify the error in this Channels consumer code:

medium📝 Debug Q14 of 15
Django - Async Django
Identify the error in this Channels consumer code:
class MyConsumer(AsyncWebsocketConsumer):
    async def connect(self):
        await self.accept

    async def receive(self, text_data):
        await self.send(text_data=text_data)
Asend method cannot send text_data
Breceive method should not be async
CMissing parentheses in await self.accept call
Dconnect method must return a value
Step-by-Step Solution
Solution:
  1. Step 1: Check connect method

    await self.accept is missing parentheses, should be await self.accept()
  2. Step 2: Validate other methods

    receive is async correctly, send accepts text_data, connect does not require return.
  3. Final Answer:

    Missing parentheses in await self.accept call -> Option C
  4. Quick Check:

    Call async methods with () [OK]
Quick Trick: Always use parentheses when awaiting methods [OK]
Common Mistakes:
MISTAKES
  • Forgetting parentheses on async method calls
  • Thinking receive can't be async
  • Expecting connect to return a value

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes