2017-09-03 16 views
0

現在、Angular 1.4.5とFirebase 4.3.0で作業しています。Angular JSを使用してFirebaseに自動的に生成されたキーを追加する

Firebaseによって自動的に生成されたキーを特定のアイテムのデータに追加する方法を知りたいと思います。

は例えば、チュートリアルに従うことによって、私は、UID

私Firebaseデータ構造使用Firebaseによって生成されたユーザID格納するために管理している:

Click here to see my Firebase data structure

をそして、これはコード

です
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を入れるようなデータに各項目のキーを追加したいのですが、どうすればよいかわかりません。

答えて

0

私はあなたの質問から理解することは、あなたがオートは、あなたがこのような何かをしたい場合は、それがで行うことができ、あなたは以下のように押して項目に

をfirebaseから鍵を生成し保存したいということです二つの方法コーディング

1)ピュアfirebase

$scope.uploadItem1 = function(){ 
    var receipeInfoKey=recipes1Ref.push().key 
    or var receipeInfoKey=ref.child('users').child('recipes').push().key; 
    var receipeInfoObj={} 
    receipeInfoObj= { 
     name: $scope.foodname, 
     category: $scope.foodcategory, 
     image: $scope.foodimage, 
     howtocook:$scope.foodhowtocook, 
     video:$scope.foodvideo, 
     date: firebase.database.ServerValue.TIMESTAMP, 
     receipeKey:receipeInfoKey 
    } 
recipes1Ref .child(receipeInfoKey).set(receipeInfoObj) 
or 
ref.child('users').child('recipes').child(receipeInfoKey).set(receipeInfoObj) 
.then(function(){ 
     $scope.foodname = ' '; 
     $scope.category = ' '; 
     $scope.foodimage = ' '; 
     $scope.foodhowtocook = ' '; 
     $scope.foodvideo = ' '; 
     $scope.message = 'Your recipes has been uploaded successfully!!'; 
    });//promise 
    }//uploadItem 

2)角度火

//このメソッドは、成功したプッシュここ

$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(recipes1Ref){ 
     var receipePushedObj= recipes1Info.$getRecord(recipes1Ref.key); 
     // recipes1Ref.key this is the auto generated key after the successful push 

     if(receipePushedObj!=null){ 
     receipePushedObj.receipeKey=recipes1Ref.key; 
     recipes1Info.$save(receipePushedObj); 
     } 
     $scope.foodname = ' '; 
     $scope.category = ' '; 
     $scope.foodimage = ' '; 
     $scope.foodhowtocook = ' '; 
     $scope.foodvideo = ' '; 
     $scope.message = 'Your recipes has been uploaded successfully!!'; 
    });//promise 

私が提案した最初の方法のワーキングサンプルがある後recipes1Infoオブジェクトを更新するようになります。 Js Fiddle Example

関連する問題