getUsername()
{
// function goes to firebase and checks the profile node for the current users username
var userId = this.Auth.auth.currentUser.uid;
return this.database.database.ref(`/profiles/${userId}/username/`).once('value').then(function(snapshot){
var username = (snapshot.val() && snapshot.val().username) || 'Anoynymous';
})
}
Ths is my code and potentially people will dislike the return Promise.reject I am doing to propagate any error.
Yours I would change…
getUsername()
{
// function goes to firebase and checks the profile node for the current users username
let userId = this.Auth.auth.currentUser.uid; // not used, so skip???
return this.database.database.ref(`/profiles/${userId}/username/`).once('value').then((snapshot) =>{
return snapshot.val() || 'Anoynymous';
})
}
So the consumer would be like (assuming getUsername is located in a provider)
Thanks very much for this Tommertom, it is the furthest i’ve got with it so far
One more question…
I have a function on one of my pages that takes user input from a form and saves the relevant data to Firebase.
It is with this data that I need to store the username… the function now looks like this…
// Function that looks at the users input and posts the request details to database
postRequestToFirebase(user: User)
{
// Take the inputs and save them into a Request object
this.request.requestId = Math.random().toString(36).substr(2, 9);
this.request.userId = this.Auth.auth.currentUser.uid;
this.request.requestText = this.designType;
this.request.bodyPosition = this.designBodyPosition;
this.request.requestHashtags = this.designHashtags;
this.request.requestDate = new Date().toLocaleString();
this.request.requestSolved = false;
// Grab the username from Firebase
this.request.username = this.data.getUsername().then(username=>{
console.log('Yeah, username ', username);
})
.catch(error=>{
console.log('OOPS, error', error)
})
// Log the request
console.log(this.request);
// Create a node on our database under requests identified with a unique request ID
this.requestObject = this.database.object(`/requests/${this.request.userId}/${this.request.requestId}`);
// Set the values of the node to our request inputs
this.requestObject.set(this.request);
}
I want to store the username string to ‘this.request.username’ before sending to Firebase.
However the username is still not saving to Firebase along with the Request objects (probably because it is not a string?), however the 'console.log('Yeah, username ‘, username)’ is working fine. Is there anyway to save this username as a string into the request.username?
// Function that looks at the users input and posts the request details to database
postRequestToFirebase(user: User)
{
// Take the inputs and save them into a Request object
this.request.requestId = Math.random().toString(36).substr(2, 9);
this.request.userId = this.Auth.auth.currentUser.uid;
this.request.requestText = this.designType;
this.request.bodyPosition = this.designBodyPosition;
this.request.requestHashtags = this.designHashtags;
this.request.requestDate = new Date().toLocaleString();
this.request.requestSolved = false;
// Grab the username from Firebase
this.data.getUsername().then(username=>{
console.log('Yeah, username ', username);
//this.request.username = username;
})
.catch(error=>{
console.log('OOPS, error', error)
})
// Log the request
console.log(this.request.username);
// Create a node on our database under requests identified with a unique request ID
this.requestObject = this.database.object(`/requests/${this.request.userId}/${this.request.requestId}`);
// Set the values of the node to our request inputs
this.requestObject.set(this.request);
}
As you can see, I am trying to fill the request object with the relevant data.
It seems that with my first ‘console.log(this.request.username)’ in the code I am getting a response of ‘undefined’…
But with the second 'console.log('Yeah, username ‘, username)’ I am getting the correct username (as shown below).
Because the request.username is undefined… it doesn’t seem to be posting to Firebase.
Please help, it is most likely because I havent set up an async function correctly.
// Function that looks at the users input and posts the request details to database
postRequestToFirebase(user: User)
{
// Take the inputs and save them into a Request object
this.request.requestId = Math.random().toString(36).substr(2, 9);
this.request.userId = this.Auth.auth.currentUser.uid;
this.request.requestText = this.designType;
this.request.bodyPosition = this.designBodyPosition;
this.request.requestHashtags = this.designHashtags;
this.request.requestDate = new Date().toLocaleString();
this.request.requestSolved = false;
// Grab the username from Firebase
this.data.getUsername().then(username=>{
console.log('Yeah, username ', username);
this.request.username = username;
// Log the request
console.log(this.request.username);
// Create a node on our database under requests identified with a unique request ID
this.requestObject = this.database.object(`/requests/${this.request.userId}/${this.request.requestId}`);
// Set the values of the node to our request inputs
this.requestObject.set(this.request);
})
.catch(error=>{
console.log('OOPS, error', error)
})
}
postRequestToFirebase(user: User) {
// Take the inputs and save them into a Request object
this.request.requestId = Math.random().toString(36).substr(2, 9);
this.request.userId = this.Auth.auth.currentUser.uid;
this.request.requestText = this.designType;
this.request.bodyPosition = this.designBodyPosition;
this.request.requestHashtags = this.designHashtags;
this.request.requestDate = new Date().toLocaleString();
this.request.requestSolved = false;
// Grab the username from Firebase
this.data.getUsername().then(username => {
console.log('Yeah, username ', username);
this.request.username = username;
// Log the request
console.log(this.request.username);
// Create a node on our database under requests identified with a unique request ID
this.requestObject = this.database.object(`/requests/${this.request.userId}/${this.request.requestId}`);
// Set the values of the node to our request inputs
this.requestObject.set(this.request);
})
.catch(error => {
console.log('OOPS, error', error)
})
}
Is really not the way to go with promises. The assignment to this.request.username must go in the then using the username variable as result from the promise resolution
(btw, there is a different way using await…async,but maybe you need to get this first!)
Now when I ‘console.log(request.username)’ I am getting an object back (see below)… So I am just working to bind this value to a string or other value that can be exported and read by Firebase.
Many thanks for your time… most wouldn’t want to waste time on an extreme novice lol.