Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs Firebase.push失败:第一个参数包含无效键($$hashKey)_Angularjs_Firebase - Fatal编程技术网

Angularjs Firebase.push失败:第一个参数包含无效键($$hashKey)

Angularjs Firebase.push失败:第一个参数包含无效键($$hashKey),angularjs,firebase,Angularjs,Firebase,我最近开始学习AngularJS+Firebase。我试图在我的firebase中写一个这样的对象: { title: "Personal Information", say: [ [{ "eng": "What's", "ukr": "Що є" }, { "eng": "your", "ukr": "твоє" }, { "eng": "surname?", "ukr": "прізвище?" }], [{ "eng": "Smith", "

我最近开始学习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": "ім'я?(не фамілія)" }]
    ]
}
行:

lessondata.add($scope.topic);
其中“lessondata”是使用angularFireCollection()和绑定到我的UI的$scope.topic-对象创建的服务。 但出现以下错误: Firebase.push失败:第一个参数在属性“say.0.0”中包含无效键($$hashKey)。键必须是非空字符串,并且不能包含“.”、“$”、“/”、“[”或“]”



据我所知,Firebase不允许使用0作为密钥,即使它是附加数组中的密钥,而零密钥是自然的。所以,我应该在一些硬编码的实例中更改对象结构,还是遗漏了什么?提前谢谢

问题是“$$hashKey”中的$,而不是0。允许0。

编辑:正如Anant在评论中指出的,在Angular(1.0.7 atm)的最新稳定版本中,您可以使用
Angular.copy(obj)
删除
$$hashkey
属性


正如Michael所说,$$hashKey中的“$”就是问题所在。Angular在后台创建
$$hashKey
属性(请参阅此处的更多信息:)。我通过做类似于
myRef.push(angular.fromJson(angular.toJson(myAngularObject))
的事情来解决这个问题,我想在这里给出另一个更简单的答案,只需在重复中使用
track by
。它将去掉引起如此多悲伤的
$$hashKey
属性

<div ng-repeat="item in items track by $index">{{item.name}}</div>
{{item.name}

您也可以在推送属性之前将其剥离

delete $scope.topic.$$hashKey

我做这件事有点晚了,但我想我应该加上我的两分钱,因为我正在摇头回答所有其他的问题。希望这能帮助你避免这个问题

使用angularFire库处理角度数据并使用其方法

是的,您可以使用纯javascript库方法来
.push().add().update()
.set()

因此,如果您想避免firebase与angular javascript通信时发生冲突,则需要使用适当的
$foo()
方法(即
$save()
)。在您的情况下,只需将
$
添加到
.add()
(使其成为
$add()

所以使用
lessondata.$add($scope.topic)



使用firebase与angularfire保存时的差异


AngularFire的
$save()
方法是使用Firebase的
set()
方法实现的。

Firebase的
push()
操作对应于AngularFire的
$add()
方法

通常,如果数据库中已有对象,或者正在处理具有自然键的对象,则应使用set()/$save()。 更多信息请点击此处:



用AngularFire注意的事项


对于回调:

如果您想让回调知道您的数据是否正确保存,您可以执行以下操作:

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
});
我很惊讶之前的回答中没有提到这一点,但看看这些文档


干杯。

摆脱$$Haskey的最佳方法是在ng重复中使用“跟踪方式”,如下所示(如上面的答案所述)

在窗体控件上。这样还可以提高angular应用程序的性能

另一种方法是删除正在使用的$$hashKey

angular.copy(someObj)

如果所有其他操作都失败,您还可以使用lodash删除以“$”开头的键


酷毙了!非常感谢你!angular.copy()也应该去掉$$hashKey属性。这有助于我在将数据推送到fb之前使用ng repeat迭代显示数据。不过,这是相当老套的=/工作完美无瑕。我甚至不知道$index是什么,但我只是重复了“跟踪$index”,这就足够了。
<div ng-repeat="(key, value) in myObj track by $index"> ... </div>
ng-model-options="{ trackBy: '$value.someKeyOnYourObject' }"
_.omitBy(yourObject, function(value, key){return _.startsWith(key, '$');});