我有一个工厂,使用firebase检查用户的authData。我想访问用户的详细信息,如姓名,电子邮件等,但我无法弄清楚如何将快照数据放入我可以使用的对象。我是Javascript的新手。
这是我的工厂:
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;
答案 0 :(得分:1)
您需要将服务注入某些内容(控制器,服务,过滤器或指令),并从控制器调用服务功能。
userService.authDataCallback()
现在您可以从控制器范围调用该函数。
FeatureCollection
答案 1 :(得分:1)
将snapshot.val()分配给局部变量data
。一旦函数结束,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
};
});
另外,您应该阅读文档中返回的服务/工厂类型。工厂的返回对象所做的事情,基本上是公开私有变量/函数以供其他模块访问。
答案 2 :(得分:0)
假设快照正确,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;
}
像这样的东西
干杯
答案 3 :(得分:0)
我认为 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()方法可能返回a 原语(字符串,数字或布尔值),数组或对象。有可能 也返回null,表示快照为空且包含 没有数据。
取自: https://www.firebase.com/docs/web/api/datasnapshot/val.html
我希望它有所帮助。
CNC中 使用此格式在控制器中获取数据:
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;
});
}
]);