问题描述
这里也提到了一个类似的问题,但是接受的答案并没有真正回答这个问题。
/ questions / 16879484 / using-angularfire-is-it-it-possible-to-to-create-relational-style-databases-or-ac>使用AngularFire,可以创建关系型数据库吗?或者访问UniqueIDs?通过AngularFire向Firebase添加嵌套的项目时,每个项目实际上都被设置在另一个按数字编号的索引下。
$ b $因此,我需要引用用户的产品与以下相对url:
users /: id / products
我的问题是,一旦我创建了一个用户如何获得索引值?
//获取用户
var ref = new Firebase('https:函数(){
// myapp.firebaseio.com/users')
var promise = angularFire(ref,$ scope,'users',{})
promise.then我可以从这里获得ID吗?
})
//用户保存产品
id = null // ???
var product = {
name:'new product',
desc:'这是一个新产品'
}
var ref = new Firebase('https: ('ref',$ scope,'products',{})
promise.then(function( ){
$ scope.products.push(product)
})
更新
为了澄清,这不是关于用户身份验证的问题。我已经有了照顾。对困惑感到抱歉。
当我开始在Firebase中的其他内容之下开始制作东西时,我刚刚遇到了一堵砖墙。如果它是用户 s或长颈鹿 s
如果我创建商店,每个商店都有产品(比方说)。
我希望能够通过
商店/ {storeId} /商品
但是 storeId 理想情况下是从AngularFire创建的索引ID(参见附图)。麻烦的是,AngularFire只是创建了这个ID,而不让我知道它。
如果我有一些成功的功能,如$ / b
$ b
success:function(response){
$ scope.store.id = response.indexId
}
这样做最有意义,但是并没有出现AngularFire准备这个非常需要的功能。请证明我错了。 :)
绝对有办法做你想要的一切。你的第一个问题是:
pre $ var $ promise $ angular $ promise.then(function(){
//可以从这里获得ID吗?
)
$ / code $ / pre
一旦这个调用的 promise 返回,你的 $ scope.users 将是用户的密钥是您创建的用户的ID值。所以要访问这些用户的ID:
pre $ var $ promise $ angular $ {$ $ $ $ $ $ $ (函数(){
为(scope.users中的var userId){
console.log(User Id:+ userId);
}
});
这对您最终想要达到的目标似乎没有太大的帮助,但至少可以看看如何从AngularFire返回所有用户的ID。
由于您要在 users I下创建产品想@bennlich试图得到这样一个事实,如果您使用的是 angularFireAuth,则用户的ID应该可以从其他变量中获得,如 user 。但是,无论何时使用该ID,都有多种方式在该用户下创建对象。给出你有用户的ID,你会有以下ref:
var userProductRef = new Firebase('https:/ /myapp.firebaseio.com/users/'+ userId +'/ products');
所以创建 product AngularFire将创建一个显式的数据绑定: angularFireCollection :
$ scope .userProducts = angularFireCollection(userProductRef);
//创建一个新产品
var newProductRef = $ scope.userProducts.add({name:'new product',desc:'This is a new product'});
console.log(New product id:+ newProductRef.name());
如果您想使用隐式数据绑定,则不需要直接使用AngularFire对象创建,因为数据同步是隐含的。在这种情况下,您必须记住,AngularFire只是对Firebase API的扩展/增补,并不是用来替代的。因此,您可能会使用Firebase .push 方法来创建ID:
//同步$ scope.userProducts和Firebase
var promise = angularFire(userProductRef,$ scope,'userProducts');
//在promise完成时创建一个新的产品
promise.then(function(){
var newProductId = userProductRef.push(); //这里是你的id
//这将自动同步到Firebase,因为隐含的angularFire绑定
$ scope.userProducts [newProductId] = {name:'new product',desc:'这是一个新产品'};
});
这些方法使用AngularFire进行对象创建,但是如前所述,我认为不要将AngularFire看作Firebase API的替代品,它使普通的Angular用例更加容易。根据视图和CRUD操作的结构,即使对于读取/更新/等操作有用,也可能不会使用AngularFire进行创建。最后要说明的是,虽然您可以使用AngularFire在Firebase中执行这种类型的关系数据结构,但以后可能会造成困难。您应该强烈考虑重组( )您的数据来优化Firebase的关键/价值商店设计。
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 databases? Or access the UniqueIDs?
When adding nested items to Firebase via AngularFire, each item is actually set under another index numbered numerically.
Because of this, I'll need to reference the user's products with the following relative url:
users/:id/products
My question is, once I have created a user (or anything for that matter), how do I get the index value?
// Get the users var ref = new Firebase('https://myapp.firebaseio.com/users') var promise = angularFire(ref, $scope, 'users', {}) promise.then(function() { // Can I get the ID from here somehow? }) // Users saves a product id = null // ??? var product = { name: 'new product', desc: 'this is a new product' } var ref = new Firebase('https://myapp.firebaseio.com/users/' + id + '/products') var promise = angularFire(ref, $scope, 'products', {}) promise.then(function() { $scope.products.push(product) })
Update
To clarify, this isn't a question about user authentication. I already have that taken care of. Sorry for the confusion.
I've just run into a brick wall when I start making things "under" other things in Firebase. Doesn't matter if it's users or giraffes.
If I make "Stores," each Store has "Products" (let's say.)
I'd like to be able to retrieve them with
stores/{storeId}/products
But the storeId would ideally be the index ID that is created from AngularFire (See the picture I have attached). The trouble is, AngularFire just creates this ID without letting me know about it.
If I had some success function like
success: function(response) { $scope.store.id = response.indexId }
That would make the most sense, but it doesn't appear AngularFire is prepare this very needed functionality. Prove me wrong, please. :)
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.
这篇关于获取Firebase AngularFire中项目的索引ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!