Retrieve data from the new Firebase - angularjs

I'm using the new Firebase at firebase.google.com for my website.
My database structure:
root
___ [auto-generated ID]
______ key1 : value
______ key2 : value
I followed the method in their document to get value of key1 as below:
database.ref('root/').on('value', function(snapshot) {
var obj = snapshot.val();
alert(obj[0].key1);
});
But I always get the error says that obj[0] is undefined and I can't read its properties. What did I do wrong and how to properly retrieve data from firebase?
Note: I'm using Angularjs for my website

Got it! alert(obj[0].key1); gives you the error!
instead, try using the below code:
database.ref('root/').on('value', function(snapshot) {
var obj = snapshot.val();
obj.forEach(function(data){
alert(data.key());
}
});
Also the usage of /root is not advised. Instead, use the url of the data.
The normal format of this url is: <uniqueAppName>.firebaseio.com
var url = "dazzling-inferno-2613.firebaseio.com";
database.ref(url).on('value', function(snapshot) {
//concepts
});

Related

how to get data from an id on a page Firebase

i have a firebase database as follows:
I wish to retrieve the information of this JSON on my page single address but I do not know how to do it to do it because I am new in angular and firebase. below my different codes
Adress list html
<ion-card *ngFor="let item of items;" (click)="editItem(item.key)">
<div><b>{{item.prenom}} {{item.nom}}</b></div>
</ion-card>
Address list .ts (method)
editItem(key){
const UserId = firebase.auth().currentUser.uid;
var usersRef = firebase.database().ref('adresse');
var adaRef = usersRef.child(UserId);
var adaFirstNameRef = adaRef.child(key);
var path = adaFirstNameRef.toString();
console.log(path);
this.router.navigate(['single-adresse/' +key]);
}
how to get the data on the unique address page. if you have examples I'm interested
I can recover the JSON link from firebase thanks to the child.
You can fetch data from firebase like this
Ref : https://firebase.google.com/docs/database/web/read-and-write
var usersRef = firebase.database().ref('adresse/'+UserId);
usersRef.once('value').then(snapshot=>{
let data = snapshot.val()
console.log(data);
// add your login here
})

Find Object Array and its Properties from Ajax Post Request

I'm sending an AJAX request to an internal PHP and receiving back an object. The object has properties of "data" and "status", yet when I try to access them, it does not return anything. How can I show each property separately?
For reference, the returned obj array is:
{"data:[{"tagId":"8787","tagDescription":"001","tagMin":"0","tagMax":"100"},{"tagId":"8729","tagDescription":"1","tagMin":"44","tagMax":"555"}]
function GetAll() {
var PostRequest ={};
PostRequest['tagId']= 'all';
$.post('php here',PostRequest,ShowAllTags);
}
function ShowAllTags( responseData, responseStatus ) {
console.log(responseStatus);
var tagData = {};
tagData = responseData;
console.log(tagData['data']);
}
So according to the above comment mention by me, The problem is with json object, in response.
So first of all fix that,
Generic solution of this problem will be;
var obj = [{"tagId":"8787","tagDescription":"001","tagMin":"0","tagMax":"100"},{"tagId":"8729","tagDescription":"1","tagMin":"44","tagMax":"555"}];
obj.forEach(function(value, index){console.log(value.tagId)});
This might help, how to get value of each property

Updating object in firebase using angular

I am using angular firebase application , a client has updated the purchase request of some commodity , using the below method in firebase ...
addPurchaseRequest(data, uid: string) {
this.db.database.ref("/purchaserequests/").push(data);
}
I got the below 2 purchases in firebase
purchaserequests
-LXZVHEpbxfpJzt_el10
actualbonus: 12312,
uid:1,
status:,
approvedon:''
-LXFSADSpbxfpJzt_el11
actualbonus: 12,
uid:2,
status:0,
approvedon:''
now i want to update the status and date for uid = 1
updatepurchaserequest(rid : string , amount : number , reason: string) {
const dataObj = {
status: 1
approvedon: new Date().toLocaleDateString()
};
this.db.database.ref("/purchaserequests/").set(dataObj);
But this is not working and replacing the complete purchase request object.
I need something like where clause of Mysql , which says update dataObj where uid =1
I think that you probably must be edit by $key or make your uid like your $key
When you get your data, you could do this:
yourData.forEach( u => {
const _data = u.payload.toJSON();
_data['$key'] = u.key;
this.inactiveUsers.unshift(_data);
})
With this, the $key is part of your data and if you want to update it, you could do this:
this.db.database.ref(purchaserequests).child($key).update(item);
And, if you want to update one or more parameters:
this.db.database.ref(`purchaserequests/${$key}`).update({ [atribute]: value });

Unable to fetch all values from a branch using Firebase with my AngularJs app

I have the following db structure in Firebase and I intend to grab all values under 'users'. I am receiving an undefined result with the following code:
fetchPharmacists() {
var userid = firebase.auth().currentUser.uid;
return firebase.database().ref().child('users').once('value').then(
function(snapshot) { snapshot.forEach(function(child) {
console.log(child.key+": "+child.val());
});
});
}
Here is the image of my firebase db structure: https://i.stack.imgur.com/HeMg5.png
fetchPharmacists() {
var userid = firebase.auth().currentUser.uid;
return firebase.database().ref().child('/users/' + userid).once('value').then(
function(snapshot) { snapshot.forEach(function(child) {
console.log(child.key+": "+child.val());
});
});
}
I just updated your code. I think you just missed to pass the uid ref. Check the below reference url.
Ref: https://firebase.google.com/docs/database/web/read-and-write
everyone I was able to achieve my intention with AngularFireDatabase using:
this.firebasedb.list("/users").valueChanges()

Firebase remove function do not work

I'm building an app with Firebase and AngularJS and I have a table with my users.
From one of my view I want to create a form permit to delete users from the Firebase table .
So I have a drop down menu with my users names and submit button.
I wrote a function to retrive the name of the user from the form and combine it with my url location of the user table, in fact the table has user name as id :
$scope.Delete_user = function(name) {
var testRef = new Firebase("https://alex-jpcreative.firebaseio.com/users")
var newRef = testRef + '/' + name;
$scope.removeUser(newRef);
}
In this function I called the removeUser one that is a function I found in Firebase doc to delete a item from the table :
$scope.removeUser = function(ref) {
ref.remove(function(error) {
alert(error ? "Uh oh!" : "Success!");
});
}
I can see the first function working fine pass the right name of users and combine it with the URL but then I have this error and it doesn't work:
TypeError: Object https://alex-jpcreative.firebaseio.com/users/Alex_dev_JPC has no method 'remove'
You need to use the child method to get the reference to the user object, rather than just appending the string to the end:
$scope.Delete_user = function(name) {
var testRef = new Firebase("https://alex-jpcreative.firebaseio.com/users");
var newRef = testRef.child(name);
$scope.removeUser(newRef);
}
See the Firebase documentation for more details.

Resources