Dave Partner

Ionic2 firebase tutorial, user not saving in database

  • 18 Replies
  • 846 Views

0 Members and 1 Guest are viewing this topic.

*

Offline bfo

  • Newbie
  • *
  • 11
    • View Profile
Ionic2 firebase tutorial, user not saving in database
« on: October 28, 2016, 04:31:16 PM »
Hi Dave, I'm still having trouble with tutorial 17 - the user is authenticated with Google but does not save to the database.. Here's the login.ts - http://pastebin.com/puTzvp5S and users-service.ts - http://pastebin.com/P7QvKMyz . I tried changing the 'this' and 'that variables' with no luck. Any suggestions? Thanks

*

Offline Dave Partner

  • Administrator
  • Full Member
  • *****
  • 109
    • View Profile
Re: Ionic2 firebase tutorial, user not saving in database
« Reply #1 on: October 29, 2016, 03:26:15 AM »
Change this, to that.


This is the function in your user-service.ts :
signUpUser(email: string , password: string){
        return this.fireAuth.createUserWithEmailAndPassword(email, password).then((newUser) => {
            //sign in the user
            this.fireAuth.signInWithEmailAndPassword(email, password).then((authenticatedUser) => {
                //successful login, create user profile
                    this.userProfile.child(authenticatedUser.uid).set({
                        email: email
                    });
                    });
        });
}



Like is said before, change it to this:
signUpUser(email: string , password: string){
var that = this;
        return this.fireAuth.createUserWithEmailAndPassword(email, password).then((newUser) => {
            //sign in the user
            that.fireAuth.signInWithEmailAndPassword(email, password).then((authenticatedUser) => {
                //successful login, create user profile
                    that.userProfile.child(authenticatedUser.uid).set({
                        email: email
                    });
                    });
        });
}
« Last Edit: October 29, 2016, 03:36:46 AM by Dave Partner »

*

Offline bfo

  • Newbie
  • *
  • 11
    • View Profile
Re: Ionic2 firebase tutorial, user not saving in database
« Reply #2 on: October 30, 2016, 02:27:52 AM »
Hi Dave, thanks for your help. It's still not working, here's my users-service.ts file - http://pastebin.com/dM9eMFPM. The user is being authenticated with Gmail, but the user and details (photo, name, etc.) are not being added to the database. The database rules are set to..




".read": "auth != null",
".write": "auth != null"


Is that correct?


Thank you

*

Offline Dave Partner

  • Administrator
  • Full Member
  • *****
  • 109
    • View Profile
Re: Ionic2 firebase tutorial, user not saving in database
« Reply #3 on: October 30, 2016, 12:57:55 PM »
That's because you are not saving anything. Update your method to this:

signUpUser(email: string , password: string){
var that = this;
        return this.fireAuth.createUserWithEmailAndPassword(email, password).then((newUser) => {
            //sign in the user
            that.fireAuth.signInWithEmailAndPassword(email, password).then((authenticatedUser) => {
                //successful login, create user profile
       /*
console.log(authenticatedUser)

The above will show you all the field names you need to use to display the values.
I'll give some examples below.
*/             that.userProfile.child(authenticatedUser.uid).set({
                        email: authenticatedUser.email,
photo: authenticatedUser.photoURL,
gender: authenticatedUser.gender
                    });
                    });
        });
}
 

*

Offline bfo

  • Newbie
  • *
  • 11
    • View Profile
Re: Ionic2 firebase tutorial, user not saving in database
« Reply #4 on: October 31, 2016, 12:24:23 AM »
Hi Dave, thanks again for your help. I changed the signUpUser function like you suggested - here's my users-service.ts file -  http://pastebin.com/DzVtDZZs.


But I'm having trouble understanding - the previous signUpUser function worked, adding the email to the database via the sign up with email method. However, the error occurred when the user was authenticated with Gmail, and would appear in the 'Authentication' table, but not the 'Database table'. Not sure what the problem is. Sorry to bother you, thanks for your help.

*

Offline Dave Partner

  • Administrator
  • Full Member
  • *****
  • 109
    • View Profile
Re: Ionic2 firebase tutorial, user not saving in database
« Reply #5 on: October 31, 2016, 05:18:30 AM »
There were multiple problems.
1. You were using 'this', instead of 'that'
2. You were singing up the user, but you weren't saving the user in the database using the right parameters that you received from Google.

You new signup method seem to work correctly now, because you now save the user credentials right.

*

Offline bfo

  • Newbie
  • *
  • 11
    • View Profile
Re: Ionic2 firebase tutorial, user not saving in database
« Reply #6 on: October 31, 2016, 04:20:36 PM »
Hi Dave, thank you very much for your help. It's still not working, I'm receiving this error in the console log - Error: Firebase.set failed: First argument contains undefined in property 'users.jXNGkO9WacXkYnxdpRMAInlYoS83.name.last'() and the command line console reads '[object Object]' when the Google authentication is submitted. Still, the user is being authenticated and their email is added to the authentication table but nothing is being added to the database. Thanks again.

*

Offline bfo

  • Newbie
  • *
  • 11
    • View Profile
Re: Ionic2 firebase tutorial, user not saving in database
« Reply #7 on: October 31, 2016, 04:29:47 PM »
I'm also getting this message in the command line console log -


   tslint: /Users/admin/Desktop/fireblogger/src/pages/login/login.ts[23, 13]: Unused property: 'users'


Here's my login.ts file - http://pastebin.com/4D3gwL3e


Thank you

*

Offline Dave Partner

  • Administrator
  • Full Member
  • *****
  • 109
    • View Profile
Re: Ionic2 firebase tutorial, user not saving in database
« Reply #8 on: November 01, 2016, 01:19:57 AM »
As the message says, you declared it but didn't use it.

So you can comment it out. No big deal.

*

Offline bfo

  • Newbie
  • *
  • 11
    • View Profile
Re: Ionic2 firebase tutorial, user not saving in database
« Reply #9 on: November 01, 2016, 03:36:53 AM »
Hi Dave, it's still not working. The user is being authenticated but the user details and email address are still not being added to the database. Not sure what the issue is. I'm getting this message in the console Error: Firebase.set failed: First argument contains undefined in property 'users.XK7mmWccLVSgomELJAJjbQYeAF52.name.last'(). Thanks for your help.
« Last Edit: November 01, 2016, 03:50:52 AM by bfo »

*

Offline Dave Partner

  • Administrator
  • Full Member
  • *****
  • 109
    • View Profile
Re: Ionic2 firebase tutorial, user not saving in database
« Reply #10 on: November 01, 2016, 11:21:05 AM »
Please copy my user authentication code on github here: github.com/daveozoalor

Its in the app/pages/user-create/ folder

*

Offline bfo

  • Newbie
  • *
  • 11
    • View Profile
Re: Ionic2 firebase tutorial, user not saving in database
« Reply #11 on: November 02, 2016, 01:43:07 AM »
Hi Dave, thanks for your help. I don't see a user-create folder in any of your projects. I copied the users-service.ts file exactly from your fireblogger project - email users are authenticated and added to the database, but Google users are authenticated and not added to the database. I've tried changing the this to that within the firebase function like you previously suggested and that also did not work. Thank you again.

*

Offline bfo

  • Newbie
  • *
  • 11
    • View Profile
Re: Ionic2 firebase tutorial, user not saving in database
« Reply #12 on: November 02, 2016, 01:53:12 AM »
This is my users-service.ts file - http://pastebin.com/EcyM8V3b. Thanks again

*

Offline Dave Partner

  • Administrator
  • Full Member
  • *****
  • 109
    • View Profile
Re: Ionic2 firebase tutorial, user not saving in database
« Reply #13 on: November 02, 2016, 09:51:34 AM »
change the below:
   that.userProfile.child(user.uid).set({
            email: user.email,
            photo: user.photoURL,
            username: user.displayName,
             name:{
                 first: res[0],
                 middle: res[1],
                 last: res[2],
            },
        });

to this:

   that.userProfile.child(user.uid).set({
            email: user.email,
            photo: user.photoURL  || '',
            username: user.displayName,
             name:{
                 first: res[0] || '',
                 middle: res[1] || '',
                 last: res[2] || '',
            },
        });

*

Offline bfo

  • Newbie
  • *
  • 11
    • View Profile
Re: Ionic2 firebase tutorial, user not saving in database
« Reply #14 on: November 02, 2016, 02:54:41 PM »
Hi Dave, that worked - the Google user was added to the database! Thanks so much for your continued help. One thing, the name is blank in the database so I guess the first middle and last were not split, but the username was added (first and last name together). Not a big deal though, I can figure that out later. Thank you again.