如果您有以前使用过的代码,其他人会来到这里
var bleh = $scope.items.$add($scope.item);
以前 $add 会返回实际值,但正如 Dan 上面提到的,它被更改为返回一个 Promise。
你有两个选择(越晚越好)。
- 回到旧版本的 angularfire。
- 您必须使用 .then 操作。
我遇到过几次这个问题,并且一直遇到这个问题(我希望可以找到更多的术语变化,如果它再次发生在我或其他人身上,它会更容易找到)。
在我的脑海中,一个承诺意味着你仍然可以使用该变量,并且在将来的某个时候它会填充它(所以我一直认为它最终会填充它并且对它感到恼火似乎从来没有)。
从这里:http://www.html5rocks.com/en/tutorials/es6/promises/
Here's how you use that promise:
promise.then(function(result) {
console.log(result); // "Stuff worked!"
}, function(err) {
console.log(err); // Error: "It broke"
});
"then" takes two arguments, a callback for a success case, and another for the failure case. Both are optional, so you can add a callback for the success or failure case only.
所以理论上,如果 firebase 在保存数据时出错(添加/删除/等),你至少应该有错误功能,这样你就可以捕捉到发生了错误。
以丹为例:
$scope.items.$add($scope.item).then(
function(p){
console.log(p.name());
},
function(err){
console.log("The expected Firebase action failed to occur this was your error: " + err);
});