Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.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
Json 如何将true返回到上面的函数_Json_Angularjs_Firebase - Fatal编程技术网

Json 如何将true返回到上面的函数

Json 如何将true返回到上面的函数,json,angularjs,firebase,Json,Angularjs,Firebase,我正在使用firebase onauth()方法,但需要将函数的值返回到调用它的上面的值。如何将true返回到上述函数,如以下代码所示: signedIn: function(){ ref.onAuth(function(authData) { if (authData) { console.log("Authenticated with uid:", authData.uid); return tru

我正在使用firebase onauth()方法,但需要将函数的值返回到调用它的上面的值。如何将true返回到上述函数,如以下代码所示:

    signedIn: function(){

        ref.onAuth(function(authData) {
          if (authData) {
            console.log("Authenticated with uid:", authData.uid);
            return true
          } else {
            console.log("Client unauthenticated.")
          }
        });
    }
使用承诺:

function signedIn(){
    var deferred = $q.defer();
    ref.onAuth(function(authData) {
      if (authData) {
        console.log("Authenticated with uid:", authData.uid);
        deferred.resolve();
        return true
      } else {
        console.log("Client unauthenticated.")
        deferred.reject();
      }
    });
    return deferred.promise;
}

signedIn().then(function () {
    alert('Authenfication successful!');
});
使用承诺:

function signedIn(){
    var deferred = $q.defer();
    ref.onAuth(function(authData) {
      if (authData) {
        console.log("Authenticated with uid:", authData.uid);
        deferred.resolve();
        return true
      } else {
        console.log("Client unauthenticated.")
        deferred.reject();
      }
    });
    return deferred.promise;
}

signedIn().then(function () {
    alert('Authenfication successful!');
});

如果onAuth是异步的(我假设是),那么您不能。。。您必须将回调传递给signedIn,或使用承诺(请参阅$q)。未立即准备好的返回值通常由
promise
对象实现。如果onAuth是异步的(我假设是),则无法。。。您必须向signedIn传递回调,或者使用承诺(请参见$q)。未立即准备好的返回值通常由
promise
对象实现。