获取 Firebase AngularFire 中项目的索引 ID

编程入门 行业动态 更新时间:2024-10-24 21:24:34
本文介绍了获取 Firebase AngularFire 中项目的索引 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

这里有人问了一个类似的问题,但接受的答案并没有真正回答这个问题.

) 您的数据以针对 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/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/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/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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-22 11:21:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1023606.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:索引   项目   Firebase   AngularFire   ID

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!