[英]Firebase database keys beginning with underscore removed
I started writing a web app and the user object has a _username
key so its on the beginning. 我开始编写一个Web应用程序,并且该用户对象具有_username
键,因此其开头是。
If I add the object to firebase database it is not there - everything else is. 如果我将对象添加到Firebase数据库中,则不存在-其他所有内容都在其中。
Why is this key/value pair not in the database? 为什么这个键/值对不在数据库中? I mean it is valid JSON, isn't it? 我的意思是它是有效的JSON,不是吗?
I mean are there other solutions than simply renaming it? 我的意思是,除了重命名外,还有其他解决方案吗?
my object which is pushed to an array has following structure: 我的被推送到数组的对象具有以下结构:
{
_username:"",
history:[],
// ...
}
My code: 我的代码:
var users = $firebaseArray(ref.child("user"));
// adding user
users.$add($scope.session.user);
// logging afterwards - also no '_username'
// logging on init
users.$loaded().then(function (data) {
$log.debug(data);
});
Everything is stored, except the underline var... 除下划线变量外,所有内容均已存储。
It looks like $add()
(or more specifically $firebaseUtils.toJSON()
) drop properties whose name starts with an _
. 看起来像$add()
(或更具体地说$firebaseUtils.toJSON()
)放置属性,其名称以_
开头。 A quick way to see this is: 一种快速的查看方法是:
console.log($firebaseUtils.toJSON(user));
A workaround would be to directly write the user object to the database through the JavaScript SDK: 一种解决方法是通过JavaScript SDK将用户对象直接写入数据库:
ref.child("user").push(user);
This does write all properties with valid key names, including the ones prefixed with a _
. 这确实会使用有效的键名来写入所有属性,包括以_
开头的键名。
Since AngularFire is built on top of the same JavaScript SDK, it will pick up the changes straight away. 由于AngularFire是基于同一JavaScript SDK构建的,因此它将立即获取更改。
Demo in jsbin: http://jsbin.com/lobadej/edit?js,console jsbin中的演示: http ://jsbin.com/lobadej/edit?js,控制台
This was a design decision, based on the fact that a common JavaScript convention for declaring private variables is to prefix them with _
. 这是一项设计决定,基于以下事实:声明私有变量的通用JavaScript约定是在它们前面加上_
。
For context on why it's different than the default SDK behavior, keep in mind that AngularFire is creating class instances that can be extended, and provide client-side functionality (not just raw JSON objects for data transfer). 关于为什么它与默认SDK行为不同的上下文,请记住AngularFire正在创建可扩展的类实例,并提供客户端功能(不仅仅是用于数据传输的原始JSON对象)。 So our goal here is to make it as simple and straightforward as possible to extend and use $firebaseArray and $firebaseObject, and to make saving and server sync as transparent as possible. 因此,我们的目标是使扩展和使用$ firebaseArray和$ firebaseObject尽可能简单明了,并使保存和服务器同步尽可能透明。
To do so, you need a simple convention to separate "local" or private things from ones you want sent to the server. 为此,您需要一个简单的约定将“本地”或私有内容与要发送到服务器的内容分开。 Since $ is technically reserved by the Angular libs, _
continues to be the appropriate way to prefix your private variables and methods pre-ES6. 由于$在技术上是Angular库保留的,因此_
继续是在ES6之前为私有变量和方法添加前缀的合适方法。
We could revisit if there were significant numbers of people having issues here, but given that it's been implemented this way nearly 3 years and this is the first comment on it, I suspect it's fine as is. 我们可以重新考虑一下是否有很多人在这里遇到问题,但是考虑到这种方式已经实施了将近3年,并且这是对此的第一条评论,我认为这样还可以。
See also: https://github.com/firebase/angularfire/issues/901 另请参阅: https : //github.com/firebase/angularfire/issues/901
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.