调用 add 后未返回 Firebase 参考 [英] Firebase Reference not returned after add has been called

查看:23
本文介绍了调用 add 后未返回 Firebase 参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这基本上是这个问题的重复(对不起复制),但它是在另一个项目中,我似乎一直对此有问题,所以我希望有人可以查看我拥有的代码并解释我一直做错的地方.

This is basically a duplicate of this question (I'm sorry for duplicating), but it is on another project, I seem to keep having issues with this, so I am hoping someone can look at the code I have and explain what I keep doing wrong.

基本上我调用 ADD 并且我没有得到返回值....我不完全确定是什么导致了它.

Basically I call ADD and I don't get a return value....I am not entirely sure what could be causing it.

function MyController($scope, $firebase, $http, $log) {
        var FB = "https://onaclovtech-home.firebaseio.com/apps/dog/"; // Enter in your FB name
        var ref = new Firebase(FB);
        var emailList = $firebase(ref);
        validateEmail = function (email) { 
           var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
           return re.test(email);
} 
        $scope.addEmail = function(name, email) {
            if (validateEmail(email))
            {
               var newResult = emailList.$add({"name" : name, "email" : email, "validated" : false});
               $log.log(newResult.toString());
               $log.log(newResult.name());

            }

        }

附加信息.这个 JSFiddle 有效http://jsfiddle.net/dLFxw/1/我无法想象发生了什么,但我只能想象它与控制器以及我如何引用 firebase 相关.

Additionaly Info. This JSFiddle works http://jsfiddle.net/dLFxw/1/ I can't imagine what is going on, but I can only imagine it's related to the controller and how I'm referencing firebase.

我认为这可能与我实施的限制(1)"有关,但这似乎没有什么区别.

I thought maybe it was related to the "limit(1)" I had implemented but that doesn't seem to make a difference.

http://jsfiddle.net/dLFxw/2/

推荐答案

AngularFire 的文档$add 的说明如下:

此方法返回一个承诺,当数据已保存到服务器时,该承诺将被履行.承诺将通过 Firebase 引用解析,您可以从中提取新添加数据的键名.

This method returns a promise that will be fulfilled when the data has been saved to the server. The promise will be resolved with a Firebase reference, from which you can extract the key name of the newly added data.

它还给出了这个方便的例子:

It also gives this handy example:

$scope.items.$add({baz: "boo"}).then(function(ref) {
  ref.name();                // Key name of the added data.
});

Ss 你可以看到,它们传入一个回调函数给 then 函数.

Ss you can see, they pass in a callback function to the then function.

因此,如果您想从服务器记录新项目的名称,您可以执行以下操作:

So if you want to log the name of the new item from the server, you'd do something like:

 emailList
   .$add({"name" : name, "email" : email, "validated" : false})
   .then(function(ref) {
     $log.log(ref.name());
   })

您的困惑似乎是由 $add 的异步性质引起的.当您调用 $add 时,会导致调用服务器.尽管 Firebase 通常非常快,但在某些情况下,此调用可能需要一些时间才能完成.这就是为什么在许多此类异步 API 中,您将传入所谓的回调函数,在本例中为 then.你可以把它读成把这个添加到项目中,然后做那个".Firebase API 将在完成后调用您的函数.

Your confusion seems caused by the asynchronous nature of the $add. When you call $add that results in a call to the server. And although Firebase is normally very fast, there may be cases when this call takes some time to complete. That's why in many of such asynchronous APIs you'll pass in a so-called callback function, in this case to then. You can read it like "Add this to the items, then do that". The Firebase API will then call your function when it completes.

这篇关于调用 add 后未返回 Firebase 参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆