我应该总是通过$ push()将新对象添加到对象列表吗? [英] Should I always add new objects to list of objects via $push()?
问题描述
我一直在通过angularfire文档工作,并对 $ push()
添加到对象的自动生成的键(name())感到困惑。方法被使用。
查看firebase网站上的一些例子,我看到一个Users对象的例子没有自动生成的关键字 - 关键字个人用户就是用户名 - 但同时每当通过 $ push
$ b将一个对象添加到数组时, $ b
我的问题是: 从 https://www.firebase.com/docs/ web / guide / saving-data.html 文档显示以下 我如何添加更多的用户,而无需用 然后一个Posts对象与生成的id: 非常感谢。 简短的回答:您可能不想使用 如果您从其他来源取得密钥,例如简单登录,几乎肯定会使用 这是因为您的用户正在进行的会话始终为您提供相同的 在这种情况下,您可以安全地使用set,而无需重置所有用户如果你设置基于那个已知的用户ID。 但是我认为你是在,所以一般来说,你什么时候设置vs推? 在这个例子中,我们使用 我们使用 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 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 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 2) What is the relationship between Example Data From https://www.firebase.com/docs/web/guide/saving-data.html The docs show the following How would I add more users without resetting my current users with And then a Posts object with the generated id: Thanks very much. The short answer: you probably don't want to use If you're getting your key from another source, like a This is because, your users' ongoing sessions always provide you with that same 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: In this example we use We use 这篇关于我应该总是通过$ push()将新对象添加到对象列表吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
$ b $ 1)我应该一直使用firebase自动生成的键吗?如果没有,那我怎么添加一个新用户,因为 $ push()
会自动创建密钥,而 $ set()
$ b $ 2 $
$示例数据 b
用户
对象:
{
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:{
-JRHTHaIs-jNPLXOQivY:{
author:gracehop,
title:宣布COBOL,一种新的编程语言
},
-JRHTHaKuITFIhnj02kE:{
author:alanisawesome,
title :图灵机
}
}
}
push
来存储你的用户。
uid
在您的Firebase中组织您的用户及其数据。
uid
,您可以使用它来查找他们的用户数据和他们的东西。
$ 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为我们生成了一个密钥。因为大部分时间我们都在处理与条目相关的注释,所以我们可以根据需要抓取所有的注释。$push()
method is used.$push
$push()
automatically creates the key, and $set()
would reset all of my users?$id
and name()
?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"
}
}
}
$set()
or adding the angularfire id with push()
?{
"posts": {
"-JRHTHaIs-jNPLXOQivY": {
"author": "gracehop",
"title": "Announcing COBOL, a New Programming Language"
},
"-JRHTHaKuITFIhnj02kE": {
"author": "alanisawesome",
"title": "The Turing Machine"
}
}
}
push
to store your users. uid
from Simple Login, you will almost certainly want to use the uid
to organize your users and their data in your firebase.uid
which you can use to look up their user data and their stuff.{
'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 ...
}
}
}
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.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.