[英]Pushing values to an object using unique ID
I am trying to push some properties and values to a Firebase object, using the Unique ID and cannot figure out how to do this. 我正在尝试使用唯一ID将一些属性和值推送到Firebase对象,但无法弄清楚该怎么做。 The object tree looks like this:
对象树如下所示:
I am referencing the projects
object using var Projects = DB.child('projects');
我使用
var Projects = DB.child('projects');
引用projects
对象var Projects = DB.child('projects');
, DB
being my Firebase database variable. ,
DB
是我的Firebase数据库变量。 When I try and access the object by the unique ID: Projects['-K1VohAnRvGfdtV6lyaH']
, the console tells me that the object is undefined. 当我尝试通过唯一ID:
Projects['-K1VohAnRvGfdtV6lyaH']
访问对象时,控制台会告诉我该对象未定义。 What am I missing here? 我在这里想念什么?
The answer was a lot easier than I was trying to make it. 答案比我尝试的要容易得多。 I just had to use the child method and post the data that way.
我只需要使用child方法并以这种方式发布数据。 Here is my code:
这是我的代码:
var projectId = $('table').attr('id');
projectId = projectId.replace('table', '');
var path = Projects.child(projectId).child('items');
path.push({
item_vendor: vendor,
item_description: description,
item_cost: cost
});
Each table has the unique ID of the project that it is displaying, so I grabbed the unique ID from there. 每个表都有它显示的项目的唯一ID,因此我从那里获取了唯一ID。 Then I just had to append the child locations to the
path
to be able to push the data where it needed to go. 然后,我只需要将子位置附加到
path
即可将数据推送到需要的位置。
ps The second child item is a property I am adding on the fly. ps第二个子项是我正在动态添加的属性。 I may could combine
projectId
and 'items'
and only use one child()
method, but it works for now. 我可以将
projectId
和'items'
组合在一起,并且只使用一个child()
方法,但是现在可以使用了。
Thanks for all of the help guys. 感谢所有帮助人员。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.