Get the id of the currently registered user

Hello,

In my I’ve been trying for 2 days now to get the id of the newly registered user but it doesn’t seem to be getting it… It inserts to my collection successfully but with a totally different Id

here’s my code:

function valuesValid  () {
 return $w('#firstName').valid &&// text input
        $w('#lastName').valid &&// text input
        $w('#artistName').valid &&// text input
        $w('#dropdown1').valid &&// dropdown
        $w('#input25').valid &&// text input
        $w('#email').valid &&// text input
        $w('#password').valid;// text input
} 

function errorMessage  () {
  $w('#errorMessage').show()// text
  $w('#button68').enable()// Button
  $w('#button68').label = 'Sign up'
  setTimeout(()=>{
  $w('#errorMessage').hide('fade')
  }, 10000)
}

export function button68_click(event) {  // Sign up button
 // This function was added from the Properties & Events panel. To learn more, visit http://wix.to/UcBnC-4
 // Add your code for this event here:
  $w('#button68').disable()
  $w('#button68').label = 'Signing you up...' 
 if (valuesValid()) {// If the input and dropdown values are valid
 let email = $w('#email').value
 let password = $w('#password').value
 // prompt the user to log in 
    wixUsers.register(email, password, {
    contactInfo: {
 "firstName": $w('#firstName').value,
 "lastName": $w('#lastName').value + " [" + $w('#artistName').value + "]",
    }
  } )
  .then( (user) => {  // 
 let resultStatus = user.status;
 let userId = user.id;  // "r5gduicme-6fem-485j-djre-4843h4kjc349"
 const toInsert = {
 "_id": userId,
 "email": $w('#email').value,
 "firstName": $w('#firstName').value,
 "lastName": $w('#lastName').value,
 "artistName" : $w('#artistName').value,
 "gender" : $w('#dropdown1').value,
 "pimage" : 'https://bon26.netlify.app/no%20profile%20picture%20soundlytude.png',
 "pwallpaper": "https://i.pinimg.com/originals/19/ea/92/19ea92a25b3867c2f0286df57cd0e0da.jpg",
 "miniBiography" : "Hey there! I'm using Soundlytude to share my music",
 "followerCount" :  0,
 "followingCount" : 0,
 "totalUploads" : 0,
          };
 // add the item to the collection
          wixData.insert("Members", toInsert)
          .catch( (err) => {// catch the inserting to collection error
        console.log(err);
         $w('#errorMessage').text = "Somehing isn't right, Failed to Insert Data...Please try again. Error: " + err
        errorMessage()
      } )
  } )
      .catch( (err) => {// If the sign up didn't work due to network connection, excessive cache etc..
        console.log(err);
        $w('#errorMessage').text = "Somehing isn't right, Couldn't sign you up...Please try again. Error: " + err
        errorMessage()
      } );
  } else{// If the input and dropdown values invalid (aren't valid)
    $w('#errorMessage').text = "Somehing isn't right, There's an Invalid or Missing Property"
    errorMessage()
  }
}

All my code is working correctly except the id is inserting the wrong one which can make me link to their profile (custom member profile) example /members/${wixUsers.currentUser.id} cuz the dynamic page id won’t be the current users id which will then result to a ‘not found’ 404 page

any help would be greatly appreciated…

DJ bon26

Try this . Worked fine for me →

wixUsers.register(email, password, {
    contactInfo: {
 "firstName": $w('#firstName').value,
 "lastName": $w('#lastName').value + " [" + $w('#artistName').value + "]",
    }
            .then((result) => {
 let user = result.user;
 let userId  =  user.id;
 });

…and the dynamic page redirection code would be more like →

wixData.insert("Members", toInsert)
 .then(() => {
  wixLocation.to(`/members/${userId}`);
 });

As the before-register userId and the after-register userId would be different

Ahhh magic…

I was getting the id only forgetting to get the user

Thank you sm <3

@okeyiwobi Did the redirection work ??

@ajithkrr Oh Yes it did thank you so much !!

I can’t get this to work at all. Here is my code snippet. ID not returned to console.

import wixUsers from ‘wix-users’ ;

$w . onReady ( function () {

});

export function bsignup_click ( event ) {
console . log($w ( ‘#ifirstname’ ). value )
// register as member using form data
wixUsers . register($w ( ‘#iemail’ ). value , $w ( ‘#ipassword’ ). value , {
contactInfo : {
“firstName” : $w ( ‘#ifirstname’ ). value ,
“lastName” : $w ( ‘#ilastname’ ). value
}
} )
. then ( ( result ) => {
let resultuser = result.user.id ;
console . log ( resultuser )
} );
}

Maybe when you’re testing the code, your’re not logged in and the permission is set to membersOnly. If this isn’t working for you, try adding a .catch so the console can tell you what the error is:

console.log($w('#ifirstname').value)
// register as member using form data
wixUsers.register($w('#iemail').value, $w('#ipassword').value, {
        contactInfo: {
            "firstName": $w('#ifirstname').value,
            "lastName": $w('#ilastname').value
        }
    })
    .then((result) => {
        let resultuser = result.user.id;
        console.log(resultuser)
    })
    .catch((err) => {
        console.log(err)
    })

Hopefully this helps

DJ bon26