Showing an element for an error

Hi guys, I’m not really experienced with web programming but I’m a student studying computer science so I’m starting to understand code(ish) when I see it, which is why I’m stumped because every forum post and documentation I’ve read indicates that the below should work! It’s basically one of those little “your email/password” is incorrect buttons that pop up when you, well, input an incorrect login.
I would love if somebody could also explain why this doesn’t work as well as helping with an answer! (but I’m also happy with a code snippet)
Thanks in advance!

import wixUsers from 'wix-users';
import wixLocation from 'wix-location';
//Should hide the error message on page load
$w.onReady(function (){
    $w("#LoginError").hide;
})
//Clicking the login will check the user database and log you in and 
//direct you to the members area should a valid login exist
$w.onReady(function(){
    $w('#loginNow').onClick(function (){
        try{
            let email = $w('#loginEmail').value;
            let password = $w('#loginPassword').value;
            wixUsers.login(email, password)
            .then(()=>{
                wixLocation.to('/members-area');
                })
        }
        //Should show the error message if email/password not found
        catch{
            $w("#LoginError").show()
        }
    })
})

You can just do the catch on the then method itself

import wixUsers from 'wix-users';
import wixLocation from 'wix-location';

$w.onReady(function(){
    $w("#LoginError").hide();
    $w('#loginNow').onClick(function (){
        
            let email = $w('#loginEmail').value;
            let password = $w('#loginPassword').value;
            wixUsers.login(email, password)
            .then(()=>{
                wixLocation.to('/members-area');
                })
        
        //Should show the error message if email/password not found
        .catch{
            $w("#LoginError").show()
        }
    })
})

Also, you only want one onReady function per page. I’ve moved the hide function into the other one (though you gotta remember paranthesis on the end of it to actually run it, otherwise you’re just refering to the function )

Hi mate, this partially helped, adding in a “try” at least made it so the red text is hidden on the page loading but now it won’t show on a failed login!
Would you be able to help?

import wixUsers from 'wix-users';
import wixLocation from 'wix-location';

$w.onReady(function(){
    $w("#LoginError").hide();
    $w('#loginNow').onClick(function (){
        try{
            let email = $w('#loginEmail').value;
            let password = $w('#loginPassword').value;
            wixUsers.login(email, password)
            .then(()=>{
                wixLocation.to('/members-area');
                })
        }
        //Should show the error message if email/password not found
        catch{
            $w("#LoginError").show();
        }
    })
})

@gamertohelllandback Hey, my bad, I was a bit to fast when copy/pasting

import wixUsers from 'wix-users';
import wixLocation from 'wix-location';

$w.onReady(function(){
    $w("#LoginError").hide();
    $w('#loginNow').onClick(function (){
        
            let email = $w('#loginEmail').value;
            let password = $w('#loginPassword').value;
            wixUsers.login(email, password)
            .then(()=>{
                wixLocation.to('/members-area');
                })
        
        //Should show the error message if email/password not found
        .catch(()=>{
            $w("#LoginError").show();
        })
    })
})

This works when I’m testing it at least. I’d forgotten the arrow function part of the catch statement in my initial answer.

@simen This hasn’t done it :grimacing: does it not require a try method for a catch to work? Also do the try’s also require the arrow part?

@gamertohelllandback No, you don’t need the try to catch it. If you check the examples in the documentation you can see that you just tag the .catch onto the then function

https://www.wix.com/velo/reference/wix-users/login

Could you link your site so I could check it out?

Also, make sure you’re not logged in when testing the code, or it won’t run.

@simen I’ve made sure to delete all none admin users so I’m not logged in, and sure but it is VERY early in development I’m just working on base functionality before worrying about it being pretty
Looking at that link would it be best to use wix-members instead of wix-users? I’m not in anyway a web developer, I’m just helping out a family member who knows even less then I do

@gamertohelllandback


I’m getting this when trying to log on with giberish. So it seems to be working for me.

It should work wether it’s wix-members or wix-users, overall wix-members are kinda better because it’s the one being updated, but if you just need basic stuff it really shouldn’t matter much, and wix-users is easier to check if a user is logged in or not with.

@simen :upside_down_face: I’m dumbass, my brain didn’t acknowledge that I’m signed in as the admin account because I’ve not got a logout functionality, I can’t find a facepalm emoji otherwise there’d be one here!
Thank you for your patience with me I appreciate the help!