$(...).toast" TypeError

I’m creating a web application with Django Channels and WebSockets. When a WebSocket connection is open, I want to dynamically create Bootstrap toasts (https://getbootstrap.com/docs/4.2/components/toasts/). However, an error is being raised: Uncaught TypeError: $(...).toast is not a function.

The Socket.onopen line $('.toast').toast('show'); is not being read correctly. I am not familiar with jQuery.

Here is a part of the js file:

 var Socket = new ReconnectingWebSocket(ws_path)
    Socket.onopen = function (e) {
      newToast();
      $('.toast').toast('show');
     };

    function newToast(data) {
        // create Bootstrap toast element
     };

The head of html file contains the following Bootstrap and jQuery files:

<head>
   <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
   <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
   <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
   <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</head>

I have already tried replacing the dollar sign with jQuery, but the same error occurs. How can I read the $('.toast').toast('show'); line in Socket.onopen? Any ideas would be appreciated.

It seems that the slim version of jQuery that you are using does not include the $.fn.toast function. You can try using the full version of jQuery instead. Simply replace this line:

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>

with this line:

<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha384-aCsoNjE9v9W6hQ5fzi0DWMt+jO4zLT7sI9TQDavEgxvJsnZZKfG5YYdXaG0fMvJ+" crossorigin="anonymous"></script>

This should provide the missing $.fn.toast function and allow your code to work properly.