javascript - Firebase return output from function -
i new in firebase , i'm trying pass $variable in function check if $variable exists.
function ifexistwaybillno(waybill_no) { var databaseref = firebase.database().ref('masterlist'); databaseref.orderbychild("waybill_no").equalto(waybill_no).on('value', function(snapshot){ alert(snapshot.exists()); //alert true or false }); } the above function work's fine when changed alert(snapshot.exists()); return snapshot.exists(); doesn't working. return undefined, should return true or false.
how can this? in advance
almost firebase asynchronous. when call function ifexistwaybillno expects immediate return, not wait. before databaseref.orderbychild("waybill_no") finished statement called function has decided return undefined.
the way fix passing callback function , using return there. exact explanation of done here: return async call.
you need rename of functions , follow syntax used there.
to start:
function(waybill_no, callback) { databaseref.orderbychild("waybill_no").equalto(waybill_no).on('value', function(snapshot) { var truth = snapshot.exists(); callback(truth); // "return" value original caller }); } remember, firebase asynchronous.
Comments
Post a Comment