Registering Multiple Members At Once

Hello.

I’m new to using Corvid, but managed to use the code listed in the wix-users API documentation to register a user (although I haven’t been able to get the phone number field to populate this way):

import wixUsers from 'wix-users';

let email = "margokw@gmail.com"
let password = "testtest"
let firstName = "MargoTest"
let lastName = "Test"
let mainPhone = "1112223333"
let nickname = "Margo"

$w.onReady(function () {

    wixUsers.register(email, password, {
            contactInfo: {
 "firstName": firstName,
 "lastName": lastName,
 "mainPhone": mainPhone,
 "nickname": nickname
            }
        })
        .then((result) => {
 let resultStatus = result.status;
        });

});

However, this code only allows me to register one user at a time (having to publish and open the site each time I want to register a new user).

Is there a better way to do this? Can I import a .csv file to the Members/PrivateMembersData database (which is the database created when you add the Members app to the front end)? When I click on Import/Export in the database right now, I only get an option to Export to file, there is no option to Import from file.

Any help would be appreciated and please pardon my newbie ignorance!

i don’t think there’s a bulk register function. But you can arrange them in an array of objects and loop through it using JavaScript.
You can also import a csv to a separate collection and add a data hook that loops through and inserts to the private/members.

Oh, the import to a separate collection and loop through sounds like a good idea. Thanks!

I’ll have to stumble through figuring out how to do that since my Javascript skills are pretty limited, but that’s a direction at least. Unless you already have some code handy that would serve as a good starter example?

I’m also not sure how that would work with the initial password creation.

Is it for one time only or you need it to work on a regular basis?

Ideally on a regular basis, but I would be totally happy with just a one-time import of members for the initial set-up. We could handle future new members another way (asking them to sign up themselves–I just didn’t want to have to ask all 250 of my current members to sign up this way).

So you can create a temporary collection called Users, have there 2 fields mail,password).
Import the csv, sync and publish
Create a web-page for admins, add a button, and use the flowing code.

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

export function button1_click(event) {
wixData.query("Users")
    .limit(1000)
    .find()
    .then((result) => {
 let items = result.items;
 for (var i = 0; i < items.length; i++){
 if (items[i] === null) continue;
            wixUsers.register(items[i].email, items[i].password);
        }
        console.log('done');
    });
}

If it makes some troubles, you may need to add a delay between the registration actions.

Thanks JD! What if I want the .csv import to contain more than just the email and password?

Do I add something like this under the if statement?

contactInfo: {
 "firstName": firstName,
 "lastName": lastName,
 "mainPhone": mainPhone,
 "nickname": nickname
            }

Yes:

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

export function button1_click(event) {
wixData.query("Users")
    .limit(1000)
    .find()
    .then((result) => {
 let items = result.items;
 for (var i = 0; i < items.length; i++){
 if (items[i] === null) continue;
 wixUsers.register(items[i].email, items[i].password, {
contactInfo: {
 "firstName": items[i].firstName,
 "lastName": items[i].lastName,
 "phones": [items[i].mainPhone],
 "nickname": items[i].nickname//check if it's possible to add nickname or not.
            }
});
        }
        console.log('done');
    });
}

Thank you so much!!!