Angularjs 如何使用Firebase将用户数据从工厂返回控制器
我有一个工厂,它使用firebase检查用户的authData。我希望访问用户的详细信息,如姓名、电子邮件等,但我不知道如何将快照数据获取到我可以使用的对象中。我是Javascript新手 这是我的工厂:Angularjs 如何使用Firebase将用户数据从工厂返回控制器,angularjs,firebase,angularfire,firebase-authentication,Angularjs,Firebase,Angularfire,Firebase Authentication,我有一个工厂,它使用firebase检查用户的authData。我希望访问用户的详细信息,如姓名、电子邮件等,但我不知道如何将快照数据获取到我可以使用的对象中。我是Javascript新手 这是我的工厂: angular.module('.....') .factory('UserDataService', function($q, $firebase, $firebaseAuth, FIREBASE_URL) { var authData = {}; function a
angular.module('.....')
.factory('UserDataService', function($q, $firebase, $firebaseAuth, FIREBASE_URL) {
var authData = {};
function authDataCallback(authData) {
if (authData) {
var ref = new Firebase(FIREBASE_URL + "/userProfiles/" + authData.uid);
ref.on("value", function(snapshot) {
var data = snapshot.val();
});
} else {
console.log("User is logged out");
}
}
// Register the callback to be fired every time auth state changes
var ref = new Firebase(FIREBASE_URL);
ref.onAuth(authDataCallback);
return authData;
});
第二次尝试:
这一次,我能够获得用户的详细信息,但它不会保存到变量服务中,而是以null返回控制器。这是我的密码:
var firebaseRef = new Firebase(FIREBASE_URL);
var authObj = $firebaseAuth(firebaseRef);
activate();
var service = {
userInfo: null
};
function activate() {
// Add listeners for authentication state changes
authObj.$onAuth(function(authData) {
if (authData) {
// Load the userInfo
loadUserInfo(authData);
} else {
// Destroy the userInfo Object if one exists
if (service.userInfo) {
service.userInfo.$destroy();
service.userInfo = null;
}
}
});
}
function loadUserInfo(authData) {
var userRef = firebaseRef.child('userProfiles').child(authData.uid);
var loadedInfo = $firebaseObject(userRef);
// console.log(loadedInfo);
loadedInfo.$loaded()
.then(function() {
service.userInfo = loadedInfo;
console.log(service.userInfo.name);
})
.catch(function(error) {
switch (error.code) {
case 'PERMISSION_DENIED':
alert('You don\'t have the permission to see that data.');
break;
default:
alert('Couldn\'t load the user info.');
}
});
}
return service;
假设快照是正确的,则val()方法将其转换为对象。 含义数据现在包含您需要的值。例如data.name 服务应该为此公开一个函数
function sendData() {
var deferred = $q.defer();
// Register the callback to be fired every time auth state changes
var ref = new Firebase(FIREBASE_URL);
ref.onAuth(function(snapshot) {
deferred.resolve(snapshot.val());
});
return deferred.promise;
}
像这样的
干杯您需要将您的服务注入到某些内容(控制器、服务、过滤器或指令)中,然后从控制器调用服务功能
.controller('myController', ['UserDataService', function($scope) {
$scope.userService = UserDataService;
}
现在,您可以从控制器作用域调用该函数
userService.authDataCallback()
将snapshot.val()分配给局部变量data
<代码>数据在函数结束后立即销毁
您需要将其分配给“外部”变量authData
,但您不能这样做,因为您有一个同名的函数参数
试着这样做:
angular.module('.....')
.factory('UserDataService', function($q, $firebase, $firebaseAuth, FIREBASE_URL) {
var authData = {};
function authDataCallback(data) {
if (data) {
var ref = new Firebase(FIREBASE_URL + "/userProfiles/" + data.uid);
ref.on("value", function(snapshot) {
authData = snapshot.val();
//[see comment] var authData = snapshot.val();
});
} else {
console.log("User is logged out");
}
}
// Register the callback to be fired every time auth state changes
var ref = new Firebase(FIREBASE_URL);
ref.onAuth(authDataCallback);
return {
authData: authData
};
});
此外,您还应阅读文档中服务/工厂的退货类型。工厂返回的对象所做的,基本上是公开私有变量/函数,供其他模块访问。我想在var data中有一个对象。 您可以使用点.field轻松访问字段。例如:
ref.once("value", function(snapshot) {
var data = snapshot.val();
// data is { "name": "Fred", "age": 53 }
// data.name === "Fred"
// data.age === 53
});
根据DataSnapshot中的数据,val()方法可能返回
基元(字符串、数字或布尔)、数组或对象。可能
还返回null,表示快照为空且包含
没有数据
摘自:
我希望有帮助
-编辑-
使用此格式获取控制器中的数据:
var app = angular.module("sampleApp", ["firebase"]);
app.factory("Auth", ["$firebaseAuth",
function($firebaseAuth) {
var ref = new Firebase("https://docs-sandbox.firebaseio.com", "example3");
return $firebaseAuth(ref);
}
]);
app.controller("SampleCtrl", ["$scope", "Auth",
function($scope, Auth) {
$scope.auth = Auth;
// any time auth status updates, add the user data to scope
$scope.auth.$onAuth(function(authData) {
$scope.authData = authData;
});
}
]);
这是一个有点不清楚(至少对我来说)你正在努力实现什么。你能试着描述一下你想要的行为/结果吗?我正试图从一个控制器中访问对象“authdata”,该控制器包含用户名email等。。。我希望将快照传递到authdata对象并返回它,您必须创建一个全局变量empty对象,如@j2L4e sayed,并在回调中分配新值。然后返回它。如何将数据变量传递到一个可以从控制器访问的对象中?我认为问题在于回调是异步的,这就是为什么会得到一个空对象。尝试将您的服务用于。然后。。。使用承诺可能会解决问题,但你必须重写所有内容。它仍然返回一个空对象;log(userService.authData);它说的是,狗屎。它是
var authData=snapshot.val()代码>在我的答案中。卸下var
!-编辑了我的答案。另外,如果这不起作用,请在设置authData={test:'test}时检查访问它是否起作用代码>是的。但是我需要在控制器中访问这些数据:例如$scope.objectname.name。我怎样才能将快照传递到我可以访问它的地方呢?首先,我认为第一次尝试更接近于我的工作尝试。我理解这个问题。解决方案是在服务中公开一个返回承诺的函数。在该函数中,订阅onAuth并在它发生时解决承诺。现在的问题是控制器将在工厂之前触发,并且对象将返回为null