简体   繁体   English

语法错误:缺少:属性ID之后

[英]Syntax Error: missing: after property id

$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. 我正在将一些数据插入Firebase数据库,但是缺少属性ID。 I looked at following question: angular js - missing : after property id 我看了以下问题: 角js-缺少:属性ID之后

But in my case phone needs to be key. 但就我而言,电话是关键。 So how do I make a object for my case? 那么,如何为我的案子做一个对象?

$scope.phone isn't a valid property name literal. $scope.phone不是有效的属性名称文字。 You could use "$scope.phone" : 您可以使用"$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. 从该字面量来看,我无法从名称中真正找到您想要的名称,也无法使phone相对于其他字段名称正常工作。


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. 请参见以下示例:usersRef.set({alanisawesome:{date_of_birth:“ 1912年6月23日”,full_name:“ Alan Turing”}而不是alaniawesome,我希望将手机作为我的钥匙。

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"] ). 之所以obj.foo ,是因为在JavaScript中,当访问对象初始化程序之外的属性名称时,可以使用点符号和文字属性名称( obj.foo ),也可以使用方括号和字符串属性名称( obj["foo"] )。 。 In the latter case, the string can be the result of any expression, including a variable reference. 在后一种情况下,字符串可以是任何表达式的结果,包括变量引用。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM