Help with outdated API code for my login system!

Help! 1/10 new sign-ups (or something like that) don’t get properly added to my CMS system!

They get registered as a Contact and as a Member in the Wix system (I see them in the dashboard), but 1/10 people don’t get added to my own custom CMS-system, called “Members”.

Since 9/10 DO get added to my system, I imagine it’s all because I’m using outdated API-calls. Right ? ? ?

Therefore I’m asking for help – what would the up-to-date code look like?

Here’s the code I’m using for the sign-up / log-in system:

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

export function loginButton_click(event) {
    // If user is logged in, log them out
    if (wixUsers.currentUser.loggedIn) {
        Promise.all([wixLocation.to('/#page1'), wixUsers.logout()])
            .then(() => {
                // update buttons accordingly
                $w("#loginButton").label = "Sign up | Login";
                $w("#profileButton").hide();
            });
    }
    // If user is not logged in:
    else {
        let userId;
        let userEmail;

        // Prompt the user to log in 
        wixUsers.promptLogin({ "mode": "login" })
            .then((user) => {
                userId = user.id;
                return user.getEmail();
            })
            .then((email) => {
                // Check if the user already exists in the collection:
                userEmail = email;
                return wixData.query("Members")
                    .eq("_id", userId)
                    .find();
            })
            .then((results) => {
                // If the user doesn't already exist, create an item in the collection
                if (results.items.length === 0) {
                    const toInsert = {
                        "_id": userId,
                        "email": userEmail
                    };
                    // Add the item to the collection
                    wixData.insert("Members", toInsert)
                        .catch((err) => {
                            console.log(err);
                        });
                }
                // Update buttons accordingly
                $w("#loginButton").label = "Log ud";
                $w("#profileButton").show();
            })
            .then(() => {
            // Redirect to a custom account page
                setTimeout(() => {
                    wixLocation.to(`/konto/update/${wixUsers.currentUser.id}`);
                }, 500);
            })
            .catch((err) => {
                console.log(err);
            });
    }
}

I hope someone can help – and please let me know if you think that the problem is something else than outdated API’s.

It so sad that I loose 1/10 or 2/10 of my customers, because they don’t get added to my custom-made “Member” CMS system…

(I can’t work with the Wix-Members-Area, so no need to mention that)

import wixUsers from ‘wix-users’;
import wixData from ‘wix-data’;
import wixLocation from ‘wix-location’;

export function loginButton_click(event) {
// If user is logged in, log them out
if (wixUsers.currentUser.loggedIn) {
Promise.all([wixLocation.to(‘/#page1’), wixUsers.logout()])
.then(() => {
// update buttons accordingly
$w(“#loginButton”).label = “Sign up | Login”;
$w(“#profileButton”).hide();
});
}
// If user is not logged in:
else {
let userId;
let userEmail;

    // Prompt the user to log in 
    wixUsers.promptLogin({ "mode": "login" })
        .then((user) => {
            userId = user.id;
            return user.getEmail();
        })
        .then((email) => {
            // Check if the user already exists in the collection:
            userEmail = email;
            return wixData.query("Members")
                .eq("_id", userId)
                .find();
        })
        .then((results) => {
            // If the user doesn't already exist, create an item in the collection
            if (results.items.length === 0) {
                const toInsert = {
                    "_id": userId,
                    "email": userEmail
                };
                // Add the item to the collection
                wixData.insert("Members", toInsert)
                    .catch((err) => {
                        console.log(err);
                    });
            }
            // Update buttons accordingly
            $w("#loginButton").label = "Log ud";
            $w("#profileButton").show();
        })
        .then(() => {
        // Redirect to a custom account page
            setTimeout(() => {
                wixLocation.to(`/konto/update/${wixUsers.currentUser.id}`);
            }, 500);
        })
        .catch((err) => {
            console.log(err);
        });
}

}