Firebase push doesn't add but replace (JavaScript) -
i'm having following code add remarks ('opm') person ('leerling').
function onformsubmitted() { event.preventdefault(); var ref = new firebase("https://mydatabase.firebaseio.com/"); var _leerling = document.getelementbyid("leerling").value; var _opmerking = document.getelementbyid("opmerking").value; var postsref = ref.child("opmerkingen"); var newpostref = postsref.push({ leerling: _leerling, opmerking: _opmerking }); var postid = newpostref.key(); var leerlingref = ref.child("leerling").child(_leerling); leerlingref.once('value', function(snapshot) { if( snapshot.val() === null ) { /* not exist */ var leerlingnummer = _leerling.substring(0,6).trim(); ref.child("leerling").child(leerlingnummer).set({ "naam": _leerling.substring(7,33).trim(), "klas": _leerling.substring(34,38) }) ref.child("leerling").child(leerlingnummer).child("opmerkingen").push({ "opmid": postid }) } else { var leerlingpostref = snapshot.ref().child("opmerkingen)"); leerlingpostref.push({"opmid": postid}); } }); }
at beginning of code postsref.push adds remark opmerkingen child (in root), intended.
at end of code same far aware (leerlingpostref.push) opmerkingen child of leerling, there whole opmerkingen child replaced, deleting existing opm: postid instead of adding one.
what wrong , how solve?
when passed leerling regular input type='text' field worked fine when passed select-option input field failed described.
solution:
var leerlingref = ref.child("leerling").child(_leerling.substring(0,6).trim());
instead of
var leerlingref = ref.child("leerling").child(_leerling);
on line 15
Comments
Post a Comment