0
$scope.messages.$add({
                    $scope.phone : {
                    username: $scope.username,
                    email: $scope.email,
                    dob: $scope.dob
                    }
                  });

I am inserting some data into Firebase database but getting missing property id. I looked at following question: angular js - missing : after property id

But in my case phone needs to be key. So how do I make a object for my case?

4
  • You can't have $scope.phone as key name Commented Sep 5, 2014 at 12:09
  • @codename-: Yes, you can. But they probably don't want to. Commented Sep 5, 2014 at 12:11
  • @T.J.Crowder : I mean that you can't have in way he write it. I your answer you put this as a string, not variable - there are two different things. Commented Sep 5, 2014 at 12:40
  • @codename-: That may be what you meant, but it's not what you said. :-) You can absolutely have the key $scope.phone. ;-) Commented Sep 5, 2014 at 12:44

2 Answers 2

1

You can't use a value as a property name in an object literal.

You would need to create the object first and use bracket syntax to set the property using the value:

var msg = {};
msg[$scope.phone] = {
  username: $scope.username,
  email: $scope.email,
  dob: $scope.dob
};
$scope.messages.$add(msg);
Sign up to request clarification or add additional context in comments.

Comments

1

$scope.phone isn't a valid property name literal. You could use "$scope.phone":

$scope.messages.$add({
    "$scope.phone" : {
        username: $scope.username,
        email: $scope.email,
        dob: $scope.dob
    }
});

...but it will create a property with that actual name, which probably isn't what you want. Looking at that literal, I can't quite figure out from the names what you actually want, I can't quite make phone work in relation to the other field names.


Re your comment:

See this example: usersRef.set({ alanisawesome: { date_of_birth: "June 23, 1912", full_name: "Alan Turing" } Instead of alaniawesome I want to have phone as my key.

You can't do that in an object initializer, the property name part of the property initializers in it are always taken literally. You'll have to do it in two separate statements:

var obj = {};
obj[alanisawesome] = { date_of_birth: "June 23, 1912", full_name: "Alan Turing" };
usersRef.set(obj);

That works because in JavaScript, when accessing a property name outside of an object initializer, you can use either dot notation and a literal property name (obj.foo), or bracketed notation and a string property name (obj["foo"]). In the latter case, the string can be the result of any expression, including a variable reference.

2 Comments

See this example: usersRef.set({ alanisawesome: { date_of_birth: "June 23, 1912", full_name: "Alan Turing" } Instead of alaniawesome I want to have phone as my key.
@rishiag: Ah, that's different. I've added to the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.