简体   繁体   English

使用唯一ID将值推入对象

[英]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: 对象树如下所示:

Firebase对象

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.

相关问题 将值推送到数组的 object - Pushing values into an object of array 数组中的所有对象都以相同的值更新,同时将具有唯一ID的对象推入数组中 - all objects in array are updating with same value while pushing the object with unique id into an array 仅将唯一值推送到数组(无重复值) - Pushing only unique values to array (no repeating values) 使用 Object.values(test[i]) 推送元素的时间复杂度 - Time complexity of pushing elements using Object.values(test[i]) 确保推送时数组中的值是唯一的 - make sure values in array are unique when pushing 将元素的值推送到 Object 的数组 - Pushing Values of an Element to an Array of Object 过滤数组并获取第一个值作为唯一ID的对象数组 - Filter array and get first values as array of object for unique id 如何获得特定键的唯一值的数组,前提是我只有使用ES6的那个对象的ID数组 - How to get an array of unique values of a specific key provided I only have an array of id's of that object using ES6 合并两个 object arrays 通过相同的 id 添加 object 值同时还添加唯一的 id - Merge two object arrays by adding object values by same id while also adding unique id 如何在每个对象值具有唯一ID的对象上进行映射并将值返回给React组件 - How to map over an object which has a unique id for each object values and return the values to a React component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM