Error accessing property in Vue 3 with Axios and Laravel Echo: Cannot read properties of undefined (reading ‘discussion_id’)

`Problem Description:

I’m working on a chat application using Vue 3, Axios, and Laravel Echo (with Pusher) for real-time updates. I’m encountering an issue where I receive the following error:

``ChatArtisanClient.vue:68 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'discussion_id')
    at ChatArtisanClient.vue:68:52
    at renderList (chunk-U6BEPC57.js?v=670d87f9:4352:16)
    at ChatArtisanClient.vue:1:1

In my onMounted lifecycle hook, I fetch initial messages and set up a listener for real-time updates as follows:`

`class MessageNotification implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $message;
    public $user;
    public $taskId;
    public $userId;

    public function __construct($message, $user, $taskId, $userId)
    {
        $this->message = $message;
        $this->user = $user;
        $this->taskId = $taskId;
        $this->userId = $userId;
    }

    public function broadcastOn()
    {
        return new PrivateChannel('task.' . $this->taskId . 'user.' . $this->userId);
    }
}`

`Broadcast::channel('App.Models.User.{id}', function ($user, $id) {
    return (int) $user->id === (int) $id;
});

use IlluminateSupportFacadesLog;

Broadcast::channel('task.{taskId}.user.{userId}', function ($user, $taskId, $userId) {
    Log::info('Tentative de connexion au canal privé', ['user_id' => $user->id, 'task_id' => $taskId, 'userId' => $userId]);

    $discussion = Discussion::where('task_id', $taskId)
                            ->where('artisan_user_id', $user->id)
                            ->first();

    if (!$discussion) {
        Log::warning('Aucune discussion trouvée', ['user_id' => $user->id, 'task_id' => $taskId]);
        return false;
    }

    Log::info('Discussion trouvée', ['discussion_id' => $discussion->id, 'task_id' => $taskId, 'artisan_user_id' => $discussion->artisan_user_id, 'client_user_id' => $discussion->user_id]);

    $isParticipant = $discussion->user_id === $userId || $discussion->artisan_user_id === $user->id;
    Log::info('Vérification des participants', ['is_participant' => $isParticipant]);

    return $isParticipant;
});
`


`use AppEventsMessageNotification;
use AppNotificationsNewMessageNotification;

class ChatsController extends Controller
{

    public function index()
    {
        $messages = Message::with('user')->get();
        return Inertia::render('Chat', compact('messages'));
    }

    public function fetchMessages()
    {
        return Message::with('user')->get();
    }


    public function store(Request $request)
    {
        // Récupérer l'utilisateur authentifié (ici l'artisan)
        $artisan = Auth::user();
        $taskId = $request->taskId;

        // Récupérer l'utilisateur (le client) associé à la tâche
        $task = Task::find($taskId);

        $userId = $task->user_id; // Identifiant du client

        // Vérifier si une discussion existe déjà entre l'artisan et le client pour la tâche
        $discussion = Discussion::where('task_id', $taskId)
                                ->where('artisan_user_id', $artisan->id)
                                ->first();

        // Si aucune discussion n'existe, en créer une nouvelle
        if (!$discussion) {
            $discussion = Discussion::create([
                'task_id' => $taskId,
                'user_id' => $userId, // Identifiant du client
                'artisan_user_id' => $artisan->id // Identifiant de l'artisan
            ]);
        }

        // Créer un nouveau message dans la discussion
        $message = $discussion->messages()->create([
            'user_id' => $artisan->id, // Identifiant de l'utilisateur envoyant le message (l'artisan ici)
            'message' => $request->message,
        ]);

         // Diffuser l'événement sur le canal privé basé sur les IDs des utilisateurs
    broadcast(new MessageNotification($message, Auth::user(), $taskId, $artisan->id))->toOthers();

        return ['status' => 'Message envoyé'];
    }
}`
`const props = defineProps({
    taskId: Number,
    userId: Number
});

const msg = ref([]);
const form = useForm({
    message: "",
    taskId: props.taskId,
});

 onMounted(async () => {
      try {
        const response = await axios.get('/messages');
        msg.value = response.data;
        console.log("MessageNotification");

        // Placez ici le reste du code qui dépend de msg.value
        console.log(msg.value);
      } catch (error) {
        console.error('Erreur lors de la récupération des messages:', error);
      }
    });


    window.Echo.private(`task.${props.taskId}.user.${props.userId}`).listen('MessageNotification', (response) => {
        console.log("MessageNotification");
        console.log(response);
        msg.value.push({
            message: response.message.message,
            user: response.user
        });
        //alert('Show without refresh!');
    });

const submit = () => {
    axios.post("/chat", form).then((response) => {
        msg.value.push(response.data.message);
        form.message = null;
    });
};
</script>

<template>
<AppLayout title="Tâche">
    <Head title="Chat" />
    <template>
        <h2 class="font-semibold text-xl text-gray-800 leading-tight">Chat</h2>
    </template>
    <div class="py-12">`your text`
        <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
            <div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
                <div class="font-bold text-xl mb-4 p-3">Messages</div>
                <hr/>
                <div class="overflow-y-auto max-h">
                    <ul class="chat list-none p-6">
                        <li v-for="(message, i) in msg" :key="i" class="left clearfix">
                            <div class="clearfix">
                                <div class="header">
                                    <strong class="text-blue-500">
                                        {{ message.discussion_id }} :
                                    </strong>
                                    {{ message.message }}
                                </div>
                            </div>
                        </li>
                    </ul>
                </div>
                <hr>
                <div class="mt-4 p-4">
                    <div class="flex items-center">
                        <input
                            id="btn-input"
                            type="text"
                            name="message"
                            class="border border-gray-200 rounded w-full"
                            v-model="form.message"
                            @keyup.enter="submit"
                            placeholder="Type your message here..."
                        />
                        <span class="ml-2">
                            <button class="bg-blue-500 text-white px-4 py-2 rounded" id="btn-chat" @click="submit">
                                Send
                            </button>
                        </span>
                    </div>
                </div>
            </div>
        </div>
    </div>
</AppLayout>
</template>`

The message already in the database are displays without any errors.

I tried to change the information retrieved and also checked the models; I expect everything is okay. I tried to display log messages, but the window.Echo part doesn’t show anything.`

New contributor

ahmadou gaye is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật