Chat Javascript does not return function

Asked

Viewed 233 times

0

I heard that the Firebase is an API that helps create real-time apps, but following a little I know about JS, I tried to create a chat. I did not understand exactly why I could not return message.

below I leave the study code:

<!DOCTYPE html>
<html>
    <head>
        <script src="https://cdn.firebase.com/js/client/1.0.17/firebase.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>

    </head>
<body>

  <div id="mensagens"></div>

  <input type="text" id="nomeusuario" placeholder="digitar nome">
  <input type="text" id="mensagem" placeholder="digitar mensagem">

    <script>
    var APP new Firebase("https://nomedomeuapp.firebaseio.com/");

        $("#mensagem").keypress(function (e) {
            if (e.Keycode == 13) {
                var = msg $("#mensagem").val()
                var = usr $("#nomeusuario").val()
                APP.push({ nomeusuario: usr, mensagem: msg });

                $("#mensagem").val('');

            }
        });

        APP.on('child_added', Function(mostra) {
              var novamensagem = mostra.val()//para receber a mensagem
              carregaMensagem(novamensagem.nomeusuario, novamensagem.mensagem);
        });

        function carregamensagem(nome, mensagem) {
            $('<div/>').text(mensagem)
            .prepend($('<strong/>').text(nome + ': '))
            .apprendTo($('#mensagens'));

            $('#mensagens')[0].scrollTop = $('#mensagens')[0].scrollHeight;
        };
    </script>

</body>
</html>

2 answers

2

I saw some mistakes:

Change this

var APP new Firebase("https://nomedomeuapp.firebaseio.com/");

therefore

var APP = new Firebase("https://nomedomeuapp.firebaseio.com/");

Change this

var = msg $("#mensagem").val()
var = usr $("#nomeusuario").val()

therefore

var msg =  $("#mensagem").val();
var usr = $("#nomeusuario").val();

change this

APP.on('child_added', Function(mostra) ...

therefore

APP.on('child_added', function(mostra) ...

The rest is correct in my view.

2

Beyond what was spoken above by Edgar, on the line where it has this:

 APP.on('child_added', Function(mostra)

change it, the word "Function" should be lowercase

APP.on('child_added', function(mostra)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.