0

var myApp = angular.module("myApp", ["firebase"]);
var nick = prompt("Anna nimesi");


myApp.controller("MyController", ["$scope", "$firebaseArray",
  function($scope, $firebaseArray) {
    var msgref = new Firebase("https://(myapp).firebaseio.com/messages");
    var usrref = new Firebase("https://(myapp).firebaseio.com/users");

    $scope.messages = $firebaseArray(msgref);
    $scope.users = $firebaseArray(usrref);

    console.log($scope.users);
    console.log($scope.messages);


    var taken = false;
    angular.forEach($scope.users, function(value, key) {
      console.log(value, key);

      if (value.username == nick) {
        taken = true;
      }


      // put your code here
    });


    if (taken == false) {
      $scope.users.$add({
        username: nick
      });
    };







    $scope.addMessage = function(e) {
      if (e.keyCode == 13 && $scope.msg) {
        var name = nick || "anynomous";


        $scope.messages.$add({
          from: name,
          body: $scope.msg
        });

        $scope.msg = "";

      }

    }


  }
])
<!DOCTYPE html>
<html ng-app="myApp">

<head>
  <link rel="stylesheet" type="text/css" href="style.css">
  <meta charset="utf-8" />
  <title>title</title>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
  <script src="https://cdn.firebase.com/js/client/2.2.4/firebase.js"></script>
  <script src="https://cdn.firebase.com/libs/angularfire/1.1.1/angularfire.min.js"></script>

</head>

<body ng-controller="MyController">
  <div class="example-chat l-demo-container">
    <ul id="example-messages" class="example-chat-messages">
      <li ng-repeat="msg in messages">
        <strong class="example-chat-username">{{msg.from}}</strong>
        {{msg.body}}
      </li>
    </ul>

  </div>
  <input class="input" autofocus="true" ng-model="msg" ng-keydown="addMessage($event)" type="text" id="messageInput" placeholder="kirjota">

  <script src="script.js"></script>
</body>

</html>

Im doing a chat, and i want to check if there is already a user with same name, so that there could not be two users with same nick. This does not work, and i can add many same nicks.

var usrref = new Firebase("https://(myapp).firebaseio.com/users");
$scope.users = $firebaseArray(usrref);

var taken = false;
for (var usr in $scope.users) {
if(usr.username == nick){
    taken = true;
};};

if(taken == false){
    $scope.users.$add({username:nick});
    };

My messages is showing perfectly with ng-repeat in html, but i cant get this work. Its obviously something simple, but i have struggled too long with this.

  • possible duplicate of [Asynchronous access to an array in Firebase](http://stackoverflow.com/questions/27049342/asynchronous-access-to-an-array-in-firebase) – Kato Jul 01 '15 at 19:23

2 Answers2

1

Since you're using Angular, I'd suggest using Angular's forEach method. Here's an example of what you could do:

angular.forEach($scope.users, function(value, key) {
   // put your code here
});

Edited

This should work for you. It checks your data after it has been loaded from Firebase. If the username is present, then it doesn't add the name. If it is not present, then it's added. You could check out the AngularFire API for $loaded().

var addNameToArray = true;

$scope.users.$loaded().then(function(data) {

   angular.forEach(data, function(value, key) {
     if(value.username === "John") {
       addNameToArray = false;
     }
   });

   if(addNameToArray) {
     $scope.users.$add({username: "John"});
   }

 });
Dr G.
  • 1,298
  • 9
  • 17
  • Using this, and move to a service to check to return a status, once loaded, works well. – Steven Scott Jun 30 '15 at 14:04
  • I think im not using these examples right. I have tried many ways how to solve this problem, but i cant get it work.. I put console.log("somthing"); inside where i should put the code, but its not even showing in the console. Sry if bad english. – ohjelmointiUkko Jun 30 '15 at 15:07
  • Instead of using `console.log("something")`, which wouldn't get you anything, try `console.log(key, value)` to see what the results are. – Dr G. Jun 30 '15 at 15:35
  • I did, but nothing happens. My whole code is in the question now, if you can say what im missing, or doing wrong. – ohjelmointiUkko Jun 30 '15 at 17:33
  • Thanks! It has been hard for me to understand angular and firebase becouse theres not much material in my language, and my (programming)english is not so good yet that i understand everything. Still have to try read more. – ohjelmointiUkko Jun 30 '15 at 20:12
0

A for (var ... in ...) iterates over object keys. So if $scope.users is an array you'll get indexes: 0, 1, 2, ... in the usr variable. To iterate over an array you can use for example following construct:

$scope.users.forEach(function(user) {

});

But maybe instead of using an array it would be better to use an object indexed by username. Then you'll get immediate information if given user exists (users[nick] !== undefined) and you'll also avoid some other trouble. See Best Practices: Arrays in Firebase.

Lukasz Wiktor
  • 19,644
  • 5
  • 69
  • 82