添加后没有返回火力地堡参考被称为 [英] Firebase Reference not returned after add has been called

查看:211
本文介绍了添加后没有返回火力地堡参考被称为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这基本上是这个副本<一个href=\"http://stackoverflow.com/questions/20874252/firebase-reference-not-returned-after-add\">question (我为复制对不起),但它的另一个项目,我似乎保持有这个问题,所以我希望有人可以看看code我已经和解释我一直做错了。

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());

            }

        }

Additionaly信息。该作品的jsfiddle
http://jsfiddle.net/dLFxw/1/
我无法想象这是怎么回事,但我只能想象它的相关的控制器以及如何我引用火力点。

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 说,对 $下面添加

该方法返回时,数据已保存到服务器,将满足的承诺。承诺将与火力地堡参考,您可以从中提取新添加的数据的键名来解决。

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你可以看到,他们在一个回调函数到然后函数传递。

所以,如果你想从服务器日志的新项目的名称,你会做这样的事情:

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());
   })

你的困惑似乎被的 $添加异步特性造成的。当你调用 $添加的结果到服务器的呼叫。而且虽然火力地堡通常非常快,有可能的情况下,当该呼叫需要一些时间来完成。这就是为什么在许多这样的异步的API,你会传递一个所谓的回调函数,在这种情况下然后。你可以喜欢读这加入的项目,然后做到这一点。然后,火力地堡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.

这篇关于添加后没有返回火力地堡参考被称为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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