0

I am working on a project. I am using Angular 2 as a front end and firebase as backend. TO access the data from Firebase i am using express. I am using REST methodology. Now all i am facing an issue here is to rest the data after i delete anything or update anything in the List. I am posting Delete and Update code. Please let me know whether i am doing any mistake or what.

router.route('/contacts/:id').get(function(req , res) {

//Show contact based on ID which is passed as perameter

var contactId = req.params.id;
contactRef.child(contactId).once('value' , function(snapshot) {
    if(snapshot.exists()) {
        var contactVal = snapshot.val();
        res.json(contactVal);
    } else {
        res.json({message : "Contact does not exist"});
    }
 })

 }).delete(function(req,res) {

//Deletes the Contact based on the ID which we are passing as perameter

var contactId = req.params.id;
console.log(contactId);
contactRef.child(contactId).remove(function(){

    console.log("deleted")

});

 res.json({message : "Contact with contact id "+ contactId +" has been removed"});
 }).put(function(req, res) {

//Updates the contact based on ID passed in the perameter.

var contactId = req.params.id;
console.log(req.body);
contactRef.child(contactId).update();
contactRef.child(contactId).once(req.body , function(snapshot) {
    //
    if(snapshot.exists()) {
        console.log("Contact Id" + contactId)
        var contactVal = snapshot.val();
        res.json({"contact ID " : contactId} );
    } else {
        res.json({message : "Contact does not exist"});
    }
  })
})

This is the code i am using but not having any idea what to do. Please let me whether i am doing anything wrong then.

The Hungry Dictator
  • 3,444
  • 5
  • 37
  • 53
  • At front-end in Angular2 you must be using two way data binding to show the data. At the time of sending delete/update request you must have track of data position in your value object. That data should be updated or removed from value object once you get success response from the server. – Ravinder Kumar Jul 15 '16 at 06:46
  • Looks like you have same problem as here: http://stackoverflow.com/questions/38370135/update-infromation-in-real-time-from-firebase-in-angularjs – Yevgen Jul 15 '16 at 07:44
  • @RavinderKumar yes its a two way data binding. Thats what i am looking for. How can i track it? – The Hungry Dictator Jul 15 '16 at 08:42
  • @Yevgen i have ued `on` earlier. but it was giving me async header error. So i am using `once` . – The Hungry Dictator Jul 15 '16 at 08:44

0 Answers0