Home > front end >  for message in messages: TypeError: 'Messages' object is not iterable
for message in messages: TypeError: 'Messages' object is not iterable

Time:10-20

Am having issues with looping through an object here is my code

from django.contrib.auth import get_user_model
import json
from asgiref.sync import async_to_sync
from channels.generic.websocket import WebsocketConsumer
from .models import Messages

User = get_user_model()

class ChatConsumer(WebsocketConsumer):

    def fetch_messages(self, data):
        messages = Messages.last_30_messages()
        content = {
            'messages': self.messages_to_json(messages)
        }
        self.send_chat_message(content)

    
    def new_messages(self, data):
        sender = data['from']
        author_user = User.objects.filter(username=sender)[0]  
        message = Messages.objects.create(sender=author_user, msg=data['message'])
        content = {
            'command': 'new_messages',
            'message': self.messages_to_json(message)
        }  
        return self.send_chat_message(content)

        
    def messages_to_json(self, messages):
        result = []
        for message in messages:
            result.append(self.messages_to_json(message))
        return result

    def message_to_json(self, message):
        return {
            'sender': message.sender.username,
            'msg': message.msg,
            'timestamp': str(message.timestamp)
        }

    command = {
        'fetch_messages': fetch_messages,
        'new_messages': new_messages
    }
    def connect(self):
        self.room_name = self.scope["url_route"]["kwargs"]["room_link"]
        self.room_group_name = "chat_%s" % self.room_name
        async_to_sync(self.channel_layer.group_add)(
            self.room_group_name,
            self.channel_name
        )
        self.accept()

    def disconnect(self, close_code):
        async_to_sync(self.channel_layer.group_discard)(
            self.room_group_name,
            self.channel_name
        )

    def receive(self, text_data):
        data = json.loads(text_data)
        self.command[data['command']](self, data)

    def send_chat_message(self, message):
        async_to_sync(self.channel_layer.group_send)(
            self.room_group_name, {
                "type": "chat_message",
                "message": message, 
                }
        )

    def send_message(self, massage):
        self.send(text_data=json.dumps(message))

    def chat_message(self, event):
        message = event["message"]

        self.send(text_data=json.dumps(message))

CodePudding user response:

I believe the problem is here

    def new_messages(self, data):
        sender = data['from']
        author_user = User.objects.filter(username=sender)[0]  
        message = Messages.objects.create(sender=author_user, msg=data['message'])
        content = {
            'command': 'new_messages',
            # here
            'message': self.messages_to_json(message)
        }  
        return self.send_chat_message(content)

i think you meant to call message_to_json instead of messages_to_json

  • Related