Set field value from member database to Mydatabase

Hello everyone ! I am trying to create a Profile database where my members will be able to share relevent informations and contact with each other, and I would like to take informations from the PrivateMember Database to put them in my Profile database and show on my dynamic page. Ideally, I wanted to show the item owner’s profile card on my dynamic page (like in the built-in profile page does) but it doesn’t seem possible (though if it is please show me !).

I tried to set field value from the input page with a code found on the community that gets the PrivateMemberdatabase infos but it doesn’t work on my live website. Please help, I have been stuck on this for weeks !

$w. onReady(function () {
$w('#dataset1'). onReady(function () {});
});
import wixData from 'wix-data';
import wixUsers from 'wix-users';

let user = wixUsers. currentUser;
let userId = user. id; // "r5cme-6fem-485j-djre-4844c49"
let isLoggedIn = user. loggedIn; // true
user. getEmail()
    . then((email) => {
 let userEmail = email; // "user@something. com"
    });

if (isLoggedIn)(
    wixData. query("Members/PrivateMembersData")
    . eq("_id", wixUsers.currentUser.id)
    . find()
    .  then((results) => {
        $w('#button3'). onClick(() => {
            $w('#dataset1'). setFieldValue("nom", results. items[0]. lastName);
            $w('#dataset1'). setFieldValue("prnom", results. items[0]. firstName);
            $w('#dataset1'). setFieldValue("privateMemberData", results. items[0]. loginEmail);
            $w("#dataset1"). save();
        });
    }));

Hello amandine,

try this one (i did not test it but should work normaly)…

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

$w.onReady(function () { 
 let user = wixUsers.currentUser;
 let userId = user.id;           
 let isLoggedIn = user.loggedIn; 
    user.getEmail()
    .then( (email) => {
 let userEmail = email;     
    } );
    $w('#button3'). onClick(() => {
        myFunction(userId)
    });
}); 


//Getting User-Info........----------------------------------------------------------------
function myFunction (userId) {
    wixData.query("PrivateMembersData")
    .eq("_id", userId)      
    .find()
    .then((results) => {
 if(results.items.length > 0) {
 let dataLength = results.items.length
 let items = results.items
 let firstItem = results.items[0]
            console.log(dataLength)
            console.log(items)
            console.log(firstItem)
            save_Data(firstItem)
        }
    })
}


function save_Data (firstItem) {
 let toInsert = {
 "nom":      firstItem.lastName,
 "prnom":    firstItem.firstName,
 "email":    firstItem.emails,
 "slug":     firstItem.emails,
    };
    wixData.insert("HereTheNameOfYourOwnProfileCollection", toInsert)
    .then( (res) => {
 let item = res;
        console.log(item) 
    })
    .catch( (err) => {
 let errorMsg = err;
    });
}

It doesn’t seem to work :confused:

@amandinehelene14
Please show your current code, and what you have done already.
Will take a look at it tommorow. :sweat_smile:

Thanks a lot, I’m pretty new at this, I just made a couple of codes beside this one, mostly on the same database, one on a hook to prevent a member to create several items, and one that sets field value on a boolean field as true or false according to which button is clicked, both tested and working fine.

For this one, the first part of the code worked to set an input field value ( using (‘#input’). value = results . items [ 0 ]. firstName ), but it doesn’t send it to the database that way so I tried this one and don’t get what’s not working…

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

$w. onReady(function () {
 let user = wixUsers. currentUser;
 let userId = user. id;
 let isLoggedIn = user. loggedIn;
    user. getEmail()
        . then((email) => {
 let userEmail = email;
        });
    $w('#button3'). onClick(() => {
        myFunction(userId)
    });
});

//Getting User-Info........----------------------------------------------------------------
function myFunction(userId) {
    wixData. query("PrivateMembersData")
        . eq("_id", userId)
        . find()
        . then((results) => {
 if (results. items. length > 0) {
 let dataLength = results. items. length
 let items = results. items
 let firstItem = results. items[0]
                console. log(dataLength)
                console. log(items)
                console. log(firstItem)
                save_Data(firstItem)
            }
        })
}

function save_Data(firstItem) {
 let toInsert = {
 "nom": firstItem. lastName,
 "prnom": firstItem. firstName,
 "mail": firstItem. email,
 "slug": firstItem. slug,
    };
    wixData. insert("Profilspersos", toInsert)
        . then((res) => {
 let item = res;
            console. log(item)
        })
        . catch((err) => {
 let errorMsg = err;
        });
}

Try this one, check also all Element/Column/Database-ID’s and names.
Everything setted up right?
All permissins in DATABASES ok ?

I have deleted 2 VALUES. Do first some tests with …

"nom":      firstItem.lastName,
"prnom":    firstItem.firstName,

…then you can expand the CODE and add the rest of values.

Improved code (not tested).

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

$w. onReady(function() {
 let user = wixUsers.currentUser;
 let userId = user.id;
 let isLoggedIn = user.loggedIn;
        user.getEmail()
        .then((email) => {
 let userEmail = email;
        });
    $w('#button3').onClick(() => {
        myFunction(userId)
    });
});

//Getting User-Info........----------------------------------------------------------------
function myFunction(userId) {console.log("START-myFunction")
    wixData.query("PrivateMembersData")
    .eq("_id", userId)
    .find()
    .then(async(results) => {
 if (results.items.length > 0) {
 let dataLength = results.items.length
 let items = results.items
 let firstItem = await results.items[0]
            console.log(dataLength)
            console.log(items)
            console.log(firstItem)  //<--- till here, everything ok, right?
            save_Data(firstItem)
        }
    })
}

//-----Inserting Data into "Profilspersos" 
async function save_Data(firstItem) {console.log("START-SAVING-PROCESS")
 let toInsert = {
 "nom":      firstItem.lastName,
 "prnom":    firstItem.firstName,
 //<---- deletet 2 VALUES . Try first with 2-items, when everything work, insert next one.
    };
 await wixData.insert("Profilspersos", toInsert) //<---- sure <---"Profilpersos" not "Profilpersons", or "Profilepersons" ?
    .then((res) => {
 let item = res;
        console.log(item)
        console.log("EVERYTHING-DONE")
    })
    .catch((err) => {
 let errorMsg = err;
    });
}

The names are as I wrote them (copied and paste from the database), I have no error message in preview, and the permissions are all on site member but it’s still not working, the database fields stay empty

@amandinehelene14
Try to REDUCE the permissions to ANYONE, just for testing and see the results?
Something changes?
Perhaps you can show the relevant database/databases in a pic?
The header of DB and the first row is needed.
Also i would like to see how is called your DATA-COLLECTION on a pic.
And if possible a pic of the RESULTS in CONSOLE.

When you got no ERROR-MESSAGES, that means, that the code is working.
Do you get the message —> “Everything is done” at the end?

Ok so, the permissions are open to anyone, no difference, and I don’t get the message at the end. Here are the pics (the website is in french):


The name of the database


The database : as I said, the field that should be filled (the first 2, first and last name), stay empty


The console (was that what you needed ?)

Alors ma cherie, ca va etre complique xDDDDDDDDDDD.
Si je regarde dans le premier CODE…

$w. onReady(function () {
$w('#dataset1'). onReady(function () {});
});
import wixData from 'wix-data';
import wixUsers from 'wix-users';

let user = wixUsers. currentUser;
let userId = user. id; // "r5cme-6fem-485j-djre-4844c49"
let isLoggedIn = user. loggedIn; // true
user. getEmail()
    . then((email) => {
 let userEmail = email; // "user@something. com"
    });

if (isLoggedIn)(
    wixData. query("Members/PrivateMembersData")
    . eq("_id", wixUsers.currentUser.id)
    . find()
    .  then((results) => {
        $w('#button3'). onClick(() => {
            $w('#dataset1'). setFieldValue("nom", results. items[0]. lastName);
            $w('#dataset1'). setFieldValue("prnom", results. items[0]. firstName);
            $w('#dataset1'). setFieldValue("privateMemberData", results. items[0]. loginEmail);
            $w("#dataset1"). save();
        });
    }));

Et, sie je regarder da ton donner image ici…

Et aussie le DATABASE-image…


Then you will recognize (tout de suite) :grin: that your DATABASE-NAME is ???
RIGHT ! —> “profil membre”

Where does …“Profilspersos” come from ???

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

$w. onReady(function () {
 let user = wixUsers. currentUser;
 let userId = user. id;
 let isLoggedIn = user. loggedIn;
    user. getEmail()
        . then((email) => {
 let userEmail = email;
        });
    $w('#button3'). onClick(() => {
        myFunction(userId)
    });
});

//Getting User-Info........----------------------------------------------------------------
function myFunction(userId) {
    wixData. query("Members/PrivateMembersData")
        . eq("_id", userId)
        . find()
        . then((results) => {
 if (results. items. length > 0) {
 let dataLength = results. items. length
 let items = results. items
 let firstItem = results. items[0]
                console. log(dataLength)
                console. log(items)
                console. log(firstItem)
                save_Data(firstItem)
            }
        })
}

function save_Data(firstItem) {
 let toInsert = {
 "nom":      firstItem.lastName,
 "prnom":    firstItem.firstName,
    };
    wixData.insert("profil membre", toInsert)
        . then((res) => {
 let item = res;
            console. log(item)
        })
        . catch((err) => {
 let errorMsg = err;
        });
}

OK, one more time… :grin: do use this CODE, because it do not throw any error.
(Si il ne sont pas des ERRORs, ca c’est bien! :stuck_out_tongue_winking_eye:)

But this time we will CHANGE the NAME of DATABASE-COLLECTION…
…from…

wixData. insert("Profilspersos", toInsert)

…to…

wixData. insert("profil membre", toInsert)

-PERMISSIONS are checked?
-Did you also SYNCED DATA between PREVIEW & LIVE ?

Controlle bien toute encore un fois madmoiselle :wink:

Good luck and happy coding!

EDIT: P.S.:

The database : as I said, the field that should be filled (the first 2, first and last name), stay empty

wixData.insert("Profilspersos", toInsert) 
wixData.insert("profil membre", toInsert)

Okayyyyy ! It finally worked ! Changing the name didn’t do it, so I figured I’d basically restart with a new database (this time keeping the name identical to the ID to avoid issues) and a new page since the problem didn’t come from the code.
I think the issue was with the button that was suppose to trigger the action, as I had attached a link to it to sent the user to another page after clicking, and it probably interfere with the code somehow.

Anyway, thanks for your help and your patience :blush: