A similar question was asked here, but the one accepted answer doesn\'t really answer the question.
Using AngularFire, is it possible to to create relational-style datab
I have the best and quickest answer for all such kind of problems guys!!!
var chru;
var ref = new Firebase("https://myapp.firebaseio.com/users/");
var sync = $firebase(ref);
var users= syncSelect.$asArray();
$scope.users= users;
users.$loaded().then(function() {
chru = users.length;
});
$scope.specificIdOnSelect = function() {
var jj;
for (jj = 0; jj < chru; jj++) {
var keyer = users.$keyAt(jj);
var rec = users.$getRecord(keyer);
if ($scope.userSelected== rec.name) {
alert("Unique Key of " + $scope.users+ " is " + users.$keyAt(jj));
}
}
};
$scope.allIds = function() {
var jj;
for (jj = 0; jj < chru; jj++) {
var keyer = users.$keyAt(jj);
var rec = users.$getRecord(keyer);
alert("Unique Key of " + $scope.users+ " is " + users.$keyAt(jj));
}
};
Try it and then please let me know if this helps!!!
In the new version of AngularFire, you can just iterate through the AngularFire object as though it were a normal javascript object.
$scope.users = {};
angularFire(ref, $scope, 'users').then(function() {
for (var id in $scope.users) {
if ($scope.users.hasOwnProperty(id) {
...
}
}
});
I don't think this is really what you want to do though, as you'd be loading all user data into every client. You should probably have the client provide a username or some other kind of id, maybe with Firebase Simple Login.
Finally, are you familiar with your browser's developer tools? They can be extremely useful for peeking inside of objects you don't know what to do with, like an AngularFire, to see how they're structured internally.
There definitely seems to be a way to do everything you'll want. Your first question was:
var promise = angularFire(ref, $scope, 'users', {})
promise.then(function() {
// Can I get the ID from here somehow?
})
Once the promise
returns for this call, your $scope.users
will be an object of users whose keys are the id values of the users you've created. So to access the ids of those users:
var promise = angularFire(ref, $scope, 'users')
promise.then(function() {
for(var userId in $scope.users){
console.log("User Id: " + userId);
}
});
This doesn't seem tremendously helpful to what you are ultimately trying to achieve, but at least you can see how to return the ids for all users from AngularFire.
Since you want to create products
under users
I think @bennlich was trying to get at the fact that the user's id should be available from some other variable, like a user
object if you are using angularFireAuth
. But whenever you do have the id, there are multiple ways to create objects under that user. Give that you have the user's id, you'll have the following ref:
var userProductRef = new Firebase('https://myapp.firebaseio.com/users/' + userId + '/products');
So one way to create a product
using AngularFire would be creating an explicit data binding with angularFireCollection
:
$scope.userProducts = angularFireCollection(userProductRef);
// create a new product
var newProductRef = $scope.userProducts.add({name: 'new product', desc: 'this is a new product'});
console.log("New product id: " + newProductRef.name());
If you wanted to use the implicit data binding, you wouldn't need to use AngularFire directly for the object creation at all, as the data sync is "implied." In this case you have to keep in mind that AngularFire is just an extension/augmentation of the vanilla Firebase API, and is not intended to be a substitute. So you might have something like this, using the Firebase .push
method to create the id:
// sync $scope.userProducts with Firebase
var promise = angularFire(userProductRef, $scope, 'userProducts');
// create a new product when promise fulfilled
promise.then(function(){
var newProductId = userProductRef.push(); // here is your id
// this will sync to Firebase automatically because of the implied angularFire binding
$scope.userProducts[newProductId] = {name: 'new product', desc: 'this is a new product'};
});
These methods use AngularFire for object creation, but as mentioned I think it helps not to think of AngularFire as a replacement for the Firebase API, it just makes common Angular use cases much easier. Depending on how your view and CRUD actions are structured, it may or may not make since to use AngularFire for Creation, even if it is useful for Read/Update/etc actions. And as a final note, while you can do this type of relational data structuring in Firebase using AngularFire, it's likely to cause difficulties later. You should strongly consider restructuring (de-normalizing) your data to optimize for Firebase's key/value store design.