我应该总是通过$ push()将新对象添加到对象列表吗? [英] Should I always add new objects to list of objects via $push()?

查看:167
本文介绍了我应该总是通过$ push()将新对象添加到对象列表吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Firebase和AngularFire创建一个由用户,帖子和评论组成的数据结构。我的印象是用户的key / id是用户名,评论和帖子的key / id是自动生成的firebase key。



我一直在通过angularfire文档工作,并对 $ push()添加到对象的自动生成的键(name())感到困惑。方法被使用。

查看firebase网站上的一些例子,我看到一个Users对象的例子没有自动生成的关键字 - 关键字个人用户就是用户名 - 但同时每当通过 $ push


$ b将一个对象添加到数组时, $ b

我的问题是:
$ b $ 1)我应该一直使用firebase自动生成的键吗?如果没有,那我怎么添加一个新用户,因为 $ push()会自动创建密钥,而 $ set() $ b $ 2 $ $ id 和<$










$示例数据 b

https://www.firebase.com/docs/ web / guide / saving-data.html



文档显示以下用户对象:

  {
users:{
alanisawesome:{
date_of_birth: 1912年6月23日,
full_name:Alan Turing
},
gracehop:{
date_of_birth:1906年12月9日,
full_name:Grace Hopper
}
}
}

我如何添加更多的用户,而无需用 $ set()重新设置我的当前用户,或者使用 push()



然后一个Posts对象与生成的id:

  {
posts:{
-JRHTHaIs-jNPLXOQivY:{
author:gracehop,
title:宣布COBOL,一种新的编程语言
},
-JRHTHaKuITFIhnj02kE:{
author:alanisawesome,
title :图灵机
}
}
}

非常感谢。

解决方案

简短的回答:您可能不想使用 push 来存储你的用户。

如果您从其他来源取得密钥,例如简单登录,几乎肯定会使用 uid 在您的Firebase中组织您的用户及其数据。



这是因为您的用户正在进行的会话始终为您提供相同的 uid ,您可以使用它来查找他们的用户数据和他们的东西。

在这种情况下,您可以安全地使用set,而无需重置所有用户如果你设置基于那个已知的用户ID。



但是我认为你是在,所以一般来说,你什么时候设置vs推?
$ b 在Firebase中,一个典型的博客可能看起来像这样:

 'users':{

//简单登录的uid,与set()一起使用
'google-1234':{
'displayName':'简Smit h',
...
}
,...
},
'posts':{

// a blog ()
'blog-post-id-i-use-in-the-url':{
'title':'Blog Post Title',
'contents':'Four score and seven ...'
},...
}
'postComments'{
'blog-post-id-i-use ($)$ {
// Firebase生成的ID通过push()完成
'_fe31ca1':{

//简单登录的uid auth)
'commenterUserId':'google-5678',
'commentBody':'cat for back for everyone!'
} ...其他评论...
}






在这个例子中,我们使用 set 当插入新的用户和帖子,因为我们从另一个来源得到一个很好的唯一的ID。这些ID是好的,因为它们允许我们稍后根据该ID容易地回想内容。



我们使用 push 为了评论,虽然。我们没有来自其他来源的良好ID,而且订单确实很重要,所以我们让Firebase为我们生成了一个密钥。因为大部分时间我们都在处理与条目相关的注释,所以我们可以根据需要抓取所有的注释。


I'm creating a data structure for Firebase and AngularFire consisting of Users, Posts, and Comments. I was under the impression that the key/id for users would be the username, and that the key/id for comments and posts would be the auto-generated firebase key.

I've been working my through the angularfire documentation and am confused about the auto-generated keys (name()) that is added to an object when the $push() method is used.

Looking at some examples on the firebase website I see that an example of a Users object does not have the auto-generated key -- the key for an individual user is the username -- but at the same time a key is added whenever you add an object to the array via $push

My question is:

1) Should I always be using the firebase auto-generated keys? And if not, then how do I add a new user since $push() automatically creates the key, and $set() would reset all of my users?

2) What is the relationship between $id and name()?

Example Data

From https://www.firebase.com/docs/web/guide/saving-data.html

The docs show the following Users object:

{
  "users": {
    "alanisawesome": {
      "date_of_birth": "June 23, 1912",
      "full_name": "Alan Turing"
    },
    "gracehop": { 
      "date_of_birth": "December 9, 1906",
      "full_name": "Grace Hopper"
    }
  }
}

How would I add more users without resetting my current users with $set() or adding the angularfire id with push()?

And then a Posts object with the generated id:

{
  "posts": {
    "-JRHTHaIs-jNPLXOQivY": {
      "author": "gracehop",
      "title": "Announcing COBOL, a New Programming Language"
    },
    "-JRHTHaKuITFIhnj02kE": {
      "author": "alanisawesome",
      "title": "The Turing Machine"
    }
  }
}

Thanks very much.

解决方案

The short answer: you probably don't want to use push to store your users.

If you're getting your key from another source, like a uid from Simple Login, you will almost certainly want to use the uid to organize your users and their data in your firebase.

This is because, your users' ongoing sessions always provide you with that same uid which you can use to look up their user data and their stuff.

And you can safely use set in this case without resetting all of your users if you set based on that known user id.

But what I think you're getting at is, So in general, when do you set vs push?

A typical blog might look something like this in Firebase:

{ 
  'users' : {

    // uid from Simple Login, that you used with set()
    'google-1234' : {  
        'displayName' : 'Jane Smith',
        ...
    } 
    , ...
  },
  'posts' : {

    // a blog post ID you pick and use for set()
    'blog-post-id-i-use-in-the-url' : { 
      'title' : 'Blog Post Title',
      'contents' : 'Four score and seven...'
     }, ...
   }
  'postComments' {
    'blog-post-id-i-use-in-the-url' : { 
      // Firebase generated ID done with push()
      '_fe31ca1' : { 

        // uid from simple login (assuming comments require auth)
        'commenterUserId': 'google-5678', 
        'commentBody': 'cats back for everyone!'
      } ... other comments ...
    }
  }
}

In this example we use set when inserting new users and posts because we get a good unique ID from another source. These IDs are good because they allow us to easily recall the content later based on that ID.

We use push for comments, though. We don't have a good ID from another source, and order does matter, so we let Firebase generate a key for us. This works out OK because most of the time we're working with comments relative to an entry, so we can just grab them all as needed.

这篇关于我应该总是通过$ push()将新对象添加到对象列表吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆