how to get data from an id on a page Firebase - angularjs

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
})

Related

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()

Retrieve data from the new Firebase

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
});

PouchDB: Multiple documents with attachments

I need to fetch several documents from my db, each one has an attachment, how can I get every document and assign its attachment dynamically and display a list using AngularJS?
Here's the bit of code I'm using:
db.getAttachment(doc._id,"image.png").then(function (blob){
var url = URL.createObjectURL(blob);
$scope.image = url;
});
How can "image.png" be a dynamic id? So I can iterate on it with a for loop
Thanks!
You could use the _attachments field and do something like:
for(var key in doc._attachments){
db.getAttachment(doc._id,key).then(function (blob){
var url = URL.createObjectURL(blob);
$scope.image = url;
});
}

AngularFire: Getting the ID of Users Data from email in SimpleLogin

I'm trying to implement some simple using registration using Firebase through AngularFire and Angular.js. I'm using the SimpleLogin tool to manage the users. I can create users just fine.
var firebaseRef = new Firebase(FIREBASE_URL);
var simpleLogin = $firebaseSimpleLogin(firebaseRef);
var firebaseUsersRef = new Firebase(FIREBASE_URL + 'users');
var firebaseUsers = $firebase(firebaseUsersRef);
var myObject = {
register: function(user) {
var myDate = new Date().getTime();
return simpleLogin.$createUser(
user.email, user.password)
.then(function(regUser) {
var userInfo = {
date: myDate,
md5: regUser.md5_hash,
firstname: user.firstname,
lastname: user.lastname,
email: user.email
}
firebaseUsers.$push(userInfo).then(function(ref) {
userInfo.uid = ref.name();
$rootScope.currentUser = userInfo;
});
}); //push user
}, //register
Works like a charm. In order to get at this information when the user logs in, I've tried implementing an event handler on the $rootscope. I would like it to search through the uid that I stored and then get me record with the right user information.
$rootScope.$on('$firebaseSimpleLogin:login', function (e, authUser) {
var query = $firebase(firebaseRef.startAt(authUser.uid).endAt(authUser.uid));
console.log(query);
$location.path('/meetings');
});
In order to use startAt and endAt, do I have to establish $priority. When I try, I get an error stating that I can't have any special characters. So that never works. I don't really care about how this data stored, I just want to get the index of the data so that I can retrieve the right user.
By using $push you tell Firebase to generate a key for you.
This is great for collections where you normally access all children at the same time. But that is not the case for your user info: you want to access the info for the current user.
So instead of using $push to add your user's info, I would use the uid of the user.
In the regular Firebase JavaScript API this can be accomplish with:
firebaseUsersRef.child(reguser.uid).set(userInfo);
The equivalent in AngularFire probably uses $set, but I don't think you have any need for that in your $createUser callback.
Update
It looks like you're trying to add your info to the existing user node that Firebase creates for you. This is the example from that from the Firebase documentation on storing user data:
myRef.child('users').child(user.uid).set({
displayName: user.displayName,
provider: user.provider,
provider_id: user.id
});
You can see that they do access the user's node using child(user.uid) similar to what I proposed.
Lessons
Two relatively small mistakes here as far as I can see:
when you use push/$push, you let Firebase generate the node name for you. In cases where there already is a globally unique ID (such as the uid of a user), you're often better off using that as the node name.
If you know the name of the node you want to retrieve, you don't need a query. You can simply access the node as ref.child(user.uid).
Thanks to Frank, I was able to figure out the right way to do this. In order to make my own users object searchable, I can use the uid from the simpleLogin object. So my register function works like this:
var firebaseRef = new Firebase(FIREBASE_URL);
var simpleLogin = $firebaseSimpleLogin(firebaseRef);
var myObject = {
register: function(user) {
var myDate = new Date().getTime();
return simpleLogin.$createUser(user.email, user.password)
.then(function(regUser) {
var userInfo = {
date: myDate,
md5: regUser.md5_hash,
firstname: user.firstname,
lastname: user.lastname,
email: user.email
}
firebaseUsers.$set(regUser.uid, userInfo);
}); //add user
}, //register
} //my Object
Using set instead of push, I can store the uid from the registered user into the object and then pass along what I want to add as the second parameter. My database will now have the users organized by uid, which can be accessed via a url.
Then, when users log in, Firebase will throw up a login event along with the authenticated user, which I can catch and use to add the current user to the $rootScope that's accessible throughout my application.
$rootScope.$on('$firebaseSimpleLogin:login', function (e, authUser) {
var ref = new Firebase(FIREBASE_URL + '/users/' + authUser.uid);
var user = $firebase(ref).$asObject();
user.$loaded().then(function() {
$rootScope.currentUser = user;
});
$location.path('/meetings');
});
Thanks for pointing me in the right direction Frank.

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