I am beginner to nodejs, angularjs and firebase world. I am trying to load data into firebase but having issue because of async nature of nodejs. My purpose is to search the existing list from firebase, if some data is there than add new element to that list and write it back to firebase. Below is my code but the issue that I am facing is little weird. It works fine and keep adding data to the array until I close that node server which is running. Once i restart that node server, because of async nature of nodejs, it add new element to array first before it gets array data from firebase.
My question is, how do we restrict that to happen? I basically wants to get the array that is stored under firebase and add one value to it on the firebase. I am trying to return this data that we receive from firebase as output of the expressjs API.
We can consider this firebase as some WS API also which get us data and insert data as well.
Please help.
var ref = new Firebase('FIREBASE URL/'+req.query.id);
var list = [];
ref.on('value', function(snap) { list = snap.val();});
list.push(req.query.searchtext);
ref.set(list);
var result = {key: req.query.id, mainList: list};
res.json(result);