現在、Angular 1.4.5とFirebase 4.3.0で作業しています。Angular JSを使用してFirebaseに自動的に生成されたキーを追加する
Firebaseによって自動的に生成されたキーを特定のアイテムのデータに追加する方法を知りたいと思います。
は例えば、チュートリアルに従うことによって、私は、UID
私Firebaseデータ構造使用Firebaseによって生成されたユーザID格納するために管理している:
をそして、これはコード
ですmyApp.factory('Authentication', ['$rootScope', '$location', '$firebaseObject','$firebaseAuth',function($rootScope, $location, $firebaseObject, $firebaseAuth){
var ref = firebase.database().ref();
var auth = $firebaseAuth();
var myObject;
auth.$onAuthStateChanged(function(authUser){
if(authUser){
var userRef = ref.child('users').child('accounts').child(authUser.uid);
var userObj = $firebaseObject(userRef);
$rootScope.currentUser = userObj;
}else {
$rootScope.currentUser = '';
}
});
myObject = {
login: function(user){
auth.$signInWithEmailAndPassword(
user.email,
user.password
).then(function(user){
$location.path('/home');
}).catch(function(error){
$rootScope.message = error.message;
});//signInWithEmailAndPassword
},//login
logout: function(){
return auth.$signOut();
}, //logout
requireAuth: function(){
return auth.$requireSignIn();
},//require Authentication
register: function(user){
auth.$createUserWithEmailAndPassword(
user.email,
user.password
).then(function(regUser){
var regRef = ref.child('users').child('accounts').child(regUser.uid).set({
date: firebase.database.ServerValue.TIMESTAMP,
regUser: regUser.uid,
firstname: user.firstname,
lastname: user.lastname,
email: user.email
}); //userinfo
myObject.login(user);
}).catch(function(error){
$rootScope.message = error.message;
});//createUserWithEmailAndPassword
}//register
};//return
return myObject;}]);//factory
これは私がFirebase(機能uploadItemとuploadItem1)に項目を追加するために使用するコードである
pageControllers.controller('UploadController',['$scope', '$firebaseAuth', '$firebaseArray',
function($scope, $firebaseAuth, $firebaseArray){
var ref = firebase.database().ref();
var auth = $firebaseAuth();
auth.$onAuthStateChanged(function(authUser){
if(authUser){
var recipesRef = ref.child('users').child('accounts').child(authUser.uid).child('recipes');
var recipesInfo = $firebaseArray(recipesRef);
var recipes1Ref = ref.child('users').child('recipes');
var recipes1Info = $firebaseArray(recipes1Ref);
var favoritesRef = ref.child('users').child('accounts').child(authUser.uid).child('favorites');
var favoritesInfo = $firebaseArray(favoritesRef);
$scope.recipes = recipesInfo;
$scope.favorites = favoritesInfo;
$scope.uploadItem = function(){
recipesInfo.$add({
name: $scope.foodname,
category: $scope.foodcategory,
image: $scope.foodimage,
howtocook:$scope.foodhowtocook,
video:$scope.foodvideo,
date: firebase.database.ServerValue.TIMESTAMP
}).then(function(){
$scope.foodname = ' ';
$scope.category = ' ';
$scope.foodimage = ' ';
$scope.foodhowtocook = ' ';
$scope.foodvideo = ' ';
$scope.message = 'Your recipes has been uploaded successfully!!';
});//promise
}//uploadItem
$scope.uploadItem1 = function(){
recipes1Info.$add({
name: $scope.foodname,
category: $scope.foodcategory,
image: $scope.foodimage,
howtocook:$scope.foodhowtocook,
video:$scope.foodvideo,
date: firebase.database.ServerValue.TIMESTAMP
}).then(function(){
$scope.foodname = ' ';
$scope.category = ' ';
$scope.foodimage = ' ';
$scope.foodhowtocook = ' ';
$scope.foodvideo = ' ';
$scope.message = 'Your recipes has been uploaded successfully!!';
});//promise
}//uploadItem
$scope.addFavorite = function(name){
favoritesInfo.$add({
name: name,
// name: $scope.itemName,
// index: $scope.itemIndex,
date: firebase.database.ServerValue.TIMESTAMP
}).then(function(){
$scope.message = 'Success!';
$('#Popup').modal();
console.log(name);
});//promise
}//uploadItem
$scope.deleteFavorite = function(key){
favoritesInfo.$remove(key);
} //deleteFavorite
$scope.deleteRecipe = function(key){
recipesInfo.$remove(key);
} //deleteFavorite
}//authUser
});//onAuthStateChanged}]);//myAll.controller
ユーザーデータにuidを入れるようなデータに各項目のキーを追加したいのですが、どうすればよいかわかりません。