I recently started learning AngularJS+Firebase. I'm trying to write in my firebase an object like this:
我最近开始学习AngularJS+Firebase。我试着在我的firebase中写一个这样的对象:
{
title: "Personal Information",
say: [
[{ "eng": "What's", "ukr": "Що є" }, { "eng": "your", "ukr": "твоє" }, { "eng": "surname?", "ukr": "прізвище?" }],
[{ "eng": "Smith", "ukr": "Сміт" }],
[{ "eng": "What's", "ukr": "Що є" }, { "eng": "your", "ukr": "твоє" }, { "eng": "first", "ukr": "перше" }, { "eng": "name?", "ukr": "ім'я?(не фамілія)" }]
]
}
with line:
线:
lessondata.add($scope.topic);
where 'lessondata' is service created with angularFireCollection() and $scope.topic - object bound to my UI. But got the following error: Firebase.push failed: first argument contains an invalid key ($$hashKey) in property 'say.0.0'. Keys must be non-empty strings and can't contain ".", "#", "$", "/", "[", or "]"
“lessondata”是使用angularfirecall()和$scope创建的服务。主题-对象绑定到我的UI。但是有以下错误:Firebase。push failed:第一个参数包含属性“say.0.0”中的无效键($$hashKey)。键必须是非空字符串,不能包含“”。、"#"、"$"、"/"、"["或"]"
As I understood Firebase do not allow to use 0 as a key even if it's a key in an attached array for which zero key is natural. So should I change my object structure in some hardcoded instance or I miss something? Thanks in advance!
正如我所理解的,Firebase不允许使用0作为键,即使它是一个附加数组中的键,而0键是自然的。我应该在硬编码的实例中改变对象结构,还是漏掉了什么?提前谢谢!
6 个解决方案
#1
24
EDIT: As Anant points out in the comments, in the latest stable version of Angular (1.0.7 atm), you can use angular.copy(obj)
to remove $$hashkey
attributes.
编辑:正如Anant在评论中指出的,在最新的稳定版(1.0.7 atm)中,可以使用Angular .copy(obj)删除$hashkey属性。
Like Michael said, the '$' in '$$hashKey' is the issue. Angular creates the $$hashKey
properties behind the scenes (see more here: https://groups.google.com/forum/#!topic/angular/pI0IgNHKjxw). I've gotten around this issue by doing something like myRef.push(angular.fromJson(angular.toJson(myAngularObject)))
.
正如迈克尔所说,“$$hashKey”中的“$”就是问题所在。角在幕后创建$$hashKey属性(在这里可以看到更多:https://groups.google.com/forum/#!topic/角/pI0IgNHKjxw)。我通过类似refmy .push(angular.fromJson(angular.toJson(myAngularObject))之类的操作解决了这个问题。
#2
6
The issue is the $ in "$$hashKey", not the 0. 0 is allowed.
问题是“$$hashKey”中的$,而不是0。0是允许的。
#3
3
I wanted to throw another answer in here that is much simpler, just use track by
in your repeat. It will get rid of the $$hashKey
attribute that is causing so much grief.
我想在这里给出另一个更简单的答案,在重复中使用track by。它将去掉导致如此多的悲伤的$hashKey属性。
<div ng-repeat="item in items track by $index">{{item.name}}</div>
#4
1
I'm a bit late to this but I thought I would like to add my two cents as I was shaking my head to all the other answers. Hopefully this can help you to avoid this issue all together.
我有点晚了,但我想我还是想补充我的两点,因为我一直在摇头。希望这能帮助你避免这个问题。
Use the angularFire library intended to handle angular data and use it's methods.
使用angularFire库来处理角度数据并使用它的方法。
while yes you can use the pure javascript library methods to .push() .add() .update()
, .set()
ect.
是的,您可以使用纯javascript库方法来。push() .add() .update(), .set()等。
So if you want to avoid any *es when firebase communicates with angular javascript you need to be using the appropriate .$foo()
methods (i.e. .$save()
). In your case just add the $
to your .add()
(make it .$add()
)
因此,如果您想避免在firebase与有棱角的javascript通信时发生冲突,您需要使用适当的.$foo()方法(即$save()))。在您的示例中,只需将$添加到.add()中(使其成为。
so use lessondata.$add($scope.topic);
所以使用lessondata。添加美元($ scope.topic);
differences when saving with firebase's vs angularfire's
使用firebase和angularfire时的不同之处。
AngularFire's $save()
method is implemented using Firebase's set()
method.
AngularFire的$save()方法是使用Firebase的set()方法实现的。
Firebase's push()
operation corresponds to AngularFire's $add()
method
Firebase的push()操作对应于AngularFire的$add()方法
Typically you should be using set()/$save() if you either have an object that already exists in the database or if you are working with objects that have a natural key. more info on that here: https://*.com/a/35959496/4642530
如果您有一个已经存在于数据库中的对象,或者您正在处理具有自然键的对象,那么通常应该使用set()/$save()。更多信息请访问:https://*.com/a/35959496/4642530
Things to note with AngularFire
要注意的事情与AngularFire
For a callback:
一个回调:
and if you want a callback to know if your data saved correctly you can do something like this:
如果你想要回调知道你的数据是否保存正确,你可以这样做:
var list = $firebaseArray(ref);
list.$add({ foo: "bar" }).then(function(ref) {
var id = ref.key();
console.log("added record with id " + id);
list.$indexFor(id); // returns location in the array
});
I was surprised this wasn't mentioned earlier in any of the other answers, but take a look at these docs https://www.firebase.com/docs/web/libraries/angular/api.html#angularfire-firebasearray-addnewdata
我很惊讶在其他答案中没有提到这一点,但是看看这些文档,https://www.firebase.com/docs/web/libraries/angular/api.html#angularfire-firebasearray-addnewdata
Cheers.
欢呼。
#5
1
The best way to get rid of the $$hasKeys is to use "track by" in ng-repeats as in the following (as mentioned in the answer above)
摆脱$$hasKeys的最好方法是在ng-repeat中使用“track by”,如下所示(如上面的答案所述)
<div ng-repeat="(key, value) in myObj track by $index"> ... </div>
but you can also set track as par of ng-model-options
但是您也可以将track设置为ng-model-options的par
ng-model-options="{ trackBy: '$value.someKeyOnYourObject' }"
on a form control. This way also improves performance of your angular app.
表单控件。这种方式也可以提高你的角化应用的性能。
Another way is to remove the $$hashKey is using
另一种方法是删除$$hashKey。
angular.copy(someObj);
angular.copy(someObj);
If all else fails, you could also use lodash to remove the keys that start with the "$".
如果所有这些都失败了,您还可以使用lodash删除以“$”开头的键。
_.omitBy(yourObject, function(value, key){return _.startsWith(key, '$');});
#6
0
You can also strip out the property before pushing it.
你也可以在推它之前剥掉它。
delete $scope.topic.$$hashKey
#1
24
EDIT: As Anant points out in the comments, in the latest stable version of Angular (1.0.7 atm), you can use angular.copy(obj)
to remove $$hashkey
attributes.
编辑:正如Anant在评论中指出的,在最新的稳定版(1.0.7 atm)中,可以使用Angular .copy(obj)删除$hashkey属性。
Like Michael said, the '$' in '$$hashKey' is the issue. Angular creates the $$hashKey
properties behind the scenes (see more here: https://groups.google.com/forum/#!topic/angular/pI0IgNHKjxw). I've gotten around this issue by doing something like myRef.push(angular.fromJson(angular.toJson(myAngularObject)))
.
正如迈克尔所说,“$$hashKey”中的“$”就是问题所在。角在幕后创建$$hashKey属性(在这里可以看到更多:https://groups.google.com/forum/#!topic/角/pI0IgNHKjxw)。我通过类似refmy .push(angular.fromJson(angular.toJson(myAngularObject))之类的操作解决了这个问题。
#2
6
The issue is the $ in "$$hashKey", not the 0. 0 is allowed.
问题是“$$hashKey”中的$,而不是0。0是允许的。
#3
3
I wanted to throw another answer in here that is much simpler, just use track by
in your repeat. It will get rid of the $$hashKey
attribute that is causing so much grief.
我想在这里给出另一个更简单的答案,在重复中使用track by。它将去掉导致如此多的悲伤的$hashKey属性。
<div ng-repeat="item in items track by $index">{{item.name}}</div>
#4
1
I'm a bit late to this but I thought I would like to add my two cents as I was shaking my head to all the other answers. Hopefully this can help you to avoid this issue all together.
我有点晚了,但我想我还是想补充我的两点,因为我一直在摇头。希望这能帮助你避免这个问题。
Use the angularFire library intended to handle angular data and use it's methods.
使用angularFire库来处理角度数据并使用它的方法。
while yes you can use the pure javascript library methods to .push() .add() .update()
, .set()
ect.
是的,您可以使用纯javascript库方法来。push() .add() .update(), .set()等。
So if you want to avoid any *es when firebase communicates with angular javascript you need to be using the appropriate .$foo()
methods (i.e. .$save()
). In your case just add the $
to your .add()
(make it .$add()
)
因此,如果您想避免在firebase与有棱角的javascript通信时发生冲突,您需要使用适当的.$foo()方法(即$save()))。在您的示例中,只需将$添加到.add()中(使其成为。
so use lessondata.$add($scope.topic);
所以使用lessondata。添加美元($ scope.topic);
differences when saving with firebase's vs angularfire's
使用firebase和angularfire时的不同之处。
AngularFire's $save()
method is implemented using Firebase's set()
method.
AngularFire的$save()方法是使用Firebase的set()方法实现的。
Firebase's push()
operation corresponds to AngularFire's $add()
method
Firebase的push()操作对应于AngularFire的$add()方法
Typically you should be using set()/$save() if you either have an object that already exists in the database or if you are working with objects that have a natural key. more info on that here: https://*.com/a/35959496/4642530
如果您有一个已经存在于数据库中的对象,或者您正在处理具有自然键的对象,那么通常应该使用set()/$save()。更多信息请访问:https://*.com/a/35959496/4642530
Things to note with AngularFire
要注意的事情与AngularFire
For a callback:
一个回调:
and if you want a callback to know if your data saved correctly you can do something like this:
如果你想要回调知道你的数据是否保存正确,你可以这样做:
var list = $firebaseArray(ref);
list.$add({ foo: "bar" }).then(function(ref) {
var id = ref.key();
console.log("added record with id " + id);
list.$indexFor(id); // returns location in the array
});
I was surprised this wasn't mentioned earlier in any of the other answers, but take a look at these docs https://www.firebase.com/docs/web/libraries/angular/api.html#angularfire-firebasearray-addnewdata
我很惊讶在其他答案中没有提到这一点,但是看看这些文档,https://www.firebase.com/docs/web/libraries/angular/api.html#angularfire-firebasearray-addnewdata
Cheers.
欢呼。
#5
1
The best way to get rid of the $$hasKeys is to use "track by" in ng-repeats as in the following (as mentioned in the answer above)
摆脱$$hasKeys的最好方法是在ng-repeat中使用“track by”,如下所示(如上面的答案所述)
<div ng-repeat="(key, value) in myObj track by $index"> ... </div>
but you can also set track as par of ng-model-options
但是您也可以将track设置为ng-model-options的par
ng-model-options="{ trackBy: '$value.someKeyOnYourObject' }"
on a form control. This way also improves performance of your angular app.
表单控件。这种方式也可以提高你的角化应用的性能。
Another way is to remove the $$hashKey is using
另一种方法是删除$$hashKey。
angular.copy(someObj);
angular.copy(someObj);
If all else fails, you could also use lodash to remove the keys that start with the "$".
如果所有这些都失败了,您还可以使用lodash删除以“$”开头的键。
_.omitBy(yourObject, function(value, key){return _.startsWith(key, '$');});
#6
0
You can also strip out the property before pushing it.
你也可以在推它之前剥掉它。
delete $scope.topic.$$hashKey