Wedadi
Wedadi

Reputation: 57

Update a specific record in Firebase using angularJS

How I can access the whole object where phoneNo = 222 and update its lang and lat

  var db = new Firebase("myFB/names");
 names {
    -K7VQBTELD4FQE1HL1U{
       phoneNo: 111,
       long: 126,
       lat: 458
     }

    -K7WJGBTELD4FQ1EHL1U{
       phoneNo: 222,
       long: 547,
       lat: 951
  }
}

Upvotes: 1

Views: 2426

Answers (1)

David East
David East

Reputation: 32604

A Firebase query requires an order by function, then you can restrict to a criteria like equalTo().

var ref = new Firebase("<my-firebase-app>/names");
var query = ref.orderyByChild('phoneNo').equalTo('222');

Then you can create a listener that will retrieve the value. The value will come back as an DataSnapshot, which contains the associated reference. Using that reference you can process whatever updates you need.

query.on('value', function(snap) {
  var obj = snap.val();
  var snapRef = snap.ref();
  snapRef.update({
    lat: 32.332325,
    long: 124.2352352
  });
});

However, you don't need to reed the object to update it. In your case, you're using a push-id as a key for /names. Perhaps an easier lookup would be the phoneNo key. Then you could do the following:

var ref = new Firebase('https://my-firebase-app/names/222');
ref.update({
  lat: 32.332325,
  long: 124.2352352
});

Upvotes: 4

Related Questions