{"id":18747,"date":"2015-03-30T16:48:21","date_gmt":"2015-03-30T11:18:21","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=18747"},"modified":"2017-04-25T09:55:39","modified_gmt":"2017-04-25T04:25:39","slug":"custom-ids-in-firebase","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/custom-ids-in-firebase\/","title":{"rendered":"Custom IDs In Firebase"},"content":{"rendered":"<p>Firebase is a service that allows us to create real-time apps without having a native back-end(server). Storing and retrieving of data in real-time is done directly from the browser(front-end), i.e. no back-end services is required to fetch the data.<\/p>\n<p>Whenever we save data in Firebase, it generates a unique identification ID for each object. Sometimes it&#8217;s hard to maintain or traverse the data on the basis of these randomly generated IDs, because the only way to access data in Firebase is via URL reference (refObject\/books\/\/title) for the particular node. So sometimes we need to store data object with our own custom keys.<\/p>\n<p>[code language=&#8221;javascript&#8221;]var onComplete = function(error) {<br \/>\n    if (error) {<br \/>\n        console.log(&#8216;Operation failed&#8217;);<br \/>\n    } else {<br \/>\n        console.log(&#8216; Operation completed&#8217;);<br \/>\n    }<br \/>\n};[\/code]<\/p>\n<p>We can save data into firebase using push() method.<\/p>\n<p>[code language=&#8221;javascript&#8221;]var booksRef = ref.child(&quot;books&quot;);<\/p>\n<p>booksRef.push({<br \/>\n    author: &quot; Adam Freeman&quot;,<br \/>\n    title: &quot;Pro AngularJS&quot;<br \/>\n}, onComplete);<\/p>\n<p>booksRef.push({<br \/>\n    author: &quot;Niall OHiggins&quot;,<br \/>\n    title: &quot;MongoDB and Python&quot;<br \/>\n}, onComplete);[\/code]<\/p>\n<p>The above code saves two objects into my data store and if the operation is successful, an auto-generated ID by Firebase is set as key for respective data objects.<br \/>\nNow, the data store in Firebase will look like:<\/p>\n<p>[code language=&#8221;javascript&#8221;]{<br \/>\n    &quot;books&quot; : {<br \/>\n    \t\t&quot;-Jldh2kvv9KyrkBuTTjV&quot; : {<br \/>\n        \t\t&quot;author&quot; : &quot; Adam Freeman&quot;,<br \/>\n           \t\t&quot;title&quot; : &quot;Pro AngularJS&quot;<br \/>\n    \t\t},<br \/>\n   \t\t &quot;-Jldh2kx0AgFXU1-Umnx&quot; : {<br \/>\n       \t\t&quot;author&quot; : &quot;Niall OHiggins&quot;,<br \/>\n           \t\t&quot;title&quot; : &quot;MongoDB and Python&quot;<br \/>\n    \t\t}<br \/>\n\t}<br \/>\n}[\/code]<\/p>\n<p>Sometimes, we need to store data with custom IDs. Such as Email-ID, UserName etc.<br \/>\nWe can use set() method to save our data in Firebase.<\/p>\n<p>[code language=&#8221;javascript&#8221;] var booksRef = ref.child(&quot;books&quot;);<br \/>\n booksRef.child(&#8216;Adam Freeman&#8217;).set({<br \/>\n     title: &quot;Pro AngularJS&quot;<br \/>\n }, onComplete);<\/p>\n<p> booksRef.child(&#8216;Niall OHiggins&#8217;).set({<br \/>\n     title: &quot;MongoDB and Python&quot;<br \/>\n }, onComplete);[\/code]<\/p>\n<p>Now, the data store in Firebase will look like :<\/p>\n<p>[code language=&#8221;javascript&#8221;]{<br \/>\n    &quot;books&quot; : {<br \/>\n    \t\t&quot;Adam Freeman&quot; : {<br \/>\n       \t\t &quot;title&quot; : &quot;Pro AngularJS&quot;<br \/>\n    \t\t},<br \/>\n    \t\t&quot;Niall OHiggins&quot; : {<br \/>\n      \t\t  &quot;title&quot; : &quot;MongoDB and Python&quot;<br \/>\n  \t  \t}<br \/>\n\t}<br \/>\n}[\/code]<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Firebase is a service that allows us to create real-time apps without having a native back-end(server). Storing and retrieving of data in real-time is done directly from the browser(front-end), i.e. no back-end services is required to fetch the data. Whenever we save data in Firebase, it generates a unique identification ID for each object. Sometimes [&hellip;]<\/p>\n","protected":false},"author":149,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":22},"categories":[1185],"tags":[1736],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/18747"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/149"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=18747"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/18747\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=18747"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=18747"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=18747"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}