Firebase和Angularfire梦魇迁移更新

时间:2015-10-05 14:57:07

标签: firebase

我是firebase的新手,我正在尝试将旧代码调整为现在已弃用的内容以及不适用的代码。我正在尝试编写一个更新一个"单个"使用现在批准的$ save()promise在我的数据源中记录,但它对我的数据源做了一些非常奇怪的事情。

我的函数(应该)允许您修改单个记录,然后更新帖子json数组。但是,它不会这样做,而是删除firebase服务器上的整个数据源,幸运的是我现在只使用testdata,因为一切都会消失。

$scope.update = function() {
  var fb = new Firebase("https://mysource.firebaseio.com/Articles/" + $scope.postToUpdate.$id);
  var article = $firebaseObject(ref);
  article.$save({
    Title: $scope.postToUpdate.Title,
    Body: $scope.postToUpdate.Body
  }).then(function(ref) {
      $('#editModal').modal('hide');
      console.log($scope.postToUpdate);
    }, function(error) {
    console.log("Error:", error);
});

}

有趣的是,我接着在控制台发出警告""我点击按钮:

使用Firebase中的数组索引存储数据可能会导致意外行为。有关详细信息,请参阅https://www.firebase.com/docs/web/guide/understanding-data.html#section-arrays-in-firebase。另请注意,您可能需要$ firebaseArray而不是$ firebaseObject。

(没有狗屎?)我在这里假设$ save()不是正确的调用,所以$ routeParams / $ firebase $ update()相当于修改数据和源代码的简单绑定?我花了好几个小时在这上面,并且真的不知道什么是正确的解决方案。

1 个答案:

答案 0 :(得分:1)

除非您遗漏了其他代码,否则您的article $ firebaseObject应该最有可能使用您之前创建的fb变量。

var article = $firebaseObject(fb);

此外,您使用$save()的方式不正确。您需要直接修改$ firebaseObject上的属性,然后在没有参数的情况下调用$save()See the docs for more.

article.Title = $scope.postToUpdate.Title;
article.Body = $scope.postToUpdate.Body;

article.$save().then(...
相关问题