-1

I am creating a simple chat application using firebase javascript api.

Here is the work flow 1.when user A wants to chat with user B first create a unique chat id

2.create a unique firebase url for these two users and user messages are stored into this.

3.display the chat messages in chat window.

Here is the code abve steps

    .controller('chatCtrl',function($scope,$stateParams,$http){
           var messagesRef;
    /*--create a unique chat-id for two users--*/     $http.get(url+'getchatid&&sender='+sender+'&&recevier='+receiver).success(function(data){
              if(data=='error'){
                alert("Error in connection try again");
              }else{
                 //create unique firebase-url for these two users
                 messagesRef= new Firebase(chaturl+data);

              }
             });
            /*---display the messages in chat window--*/
             messagesRef.on('value', function (snapshot) {
                       var  msg= snapshot.val();
                       if(msg!=null){
                        $scope.messages=msg;
                       }else{
                         $scope.messages='';
                       }
                });
             /*--add chat messages--*/
             $scope.sendmessage=function(user){
               if(!user||!user.message){
                 alert("Message required");
               }else{
                 messagesRef.push({text:user.message});
                 user.message='';
               }
             }          
          });

Problem is i cant implement the step-3,step-1 and 2 is works fine.

When trying to display the messages i will get the following error

TypeError: Cannot read property 'on' of undefined

I think problem is associated with this code

messagesRef.on('value', function (snapshot) {
               var  msg= snapshot.val();
               if(msg!=null){
                $scope.messages=msg;
               }else{
                 $scope.messages='';
               }
        });
Blessan Kurien
  • 1,645
  • 9
  • 31
  • 63

1 Answers1

0

Modified your code

        controller('chatCtrl', function ($scope, $stateParams, $http) {
            var messagesRef;
            $http.get(url + 'getchatid&&sender=' + sender + '&&recevier=' + receiver).success(function (data) {
                if (data === 'error') {
                    alert("Error in connection try again");
                } else {
                    messagesRef = new Firebase(chaturl + data);
                    messagesRef.on('value', function (snapshot) {
                        var msg = snapshot.val();
                        if (msg !== null) {
                            $scope.messages = msg;
                        } else {
                            $scope.messages = '';
                        }
                        $scope.$apply();
                    });

                }
            });
            $scope.sendmessage = function (user) {
                if (!user || !user.message) {
                    alert("Message required");
                } else if (messagesRef) {
                    messagesRef.push({text: user.message});
                    user.message = '';
                }
            };
        });
Santanu Kumar
  • 410
  • 3
  • 15