javascript - Firebase push mutiple objects with keys -


im not sure how calls, want push data :

by using .push()

{     '{this_should_be_a_key}' : {         name : '',         items : {             '{this_should_be_a_key}' : {                 title : ''             }         }     } } 

now : should promise

ref.push({     'name' : '' }).then(function(snapshot) {     let key = snapshot.key;     ref.child(key+'/items').push({         title : ''     }); }).catch(function(err) {     console.log(err); }); 

this i have push 2 times, easier way or its possible push 1 time ? like..

ref.push({     'name' : '',     'items' : {         '{this_should_be_a_key}' {             'title' : ''         }     } }).then(function(snapshot) {     alert('done'); }).catch(function(err) {     console.log(err); }); 

firebase's push keys generated on client , can generate 1 calling push no arguments. return reference , key generated push key. entirely client-side , not involve communicating server.

so can generate key, prepare data , call push again:

var key = ref.push().key; var data = {   name: '',   items: {} }; data.items[key] = { title: '' };  ref.push(data)   .then(function () { console.log('pushed'); });   .catch(function (error) { console.log(error); }); 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -