Put value in database

Hello, I would like how can I put a value in my database.
Next, It’s my code,

import wixUsers from ‘wix-users’;
import wixData from ‘wix-data’;
$w.onReady( function () {
//TODO: write your page related code here…
});
export function button1_click() {
// create an item
wixData.query(“Members/PrivateMembersData”)
.eq(“_id”, wixUsers.currentUser.id)
.find()
.then((results) => {
//script here
$w(‘#text22’).text = results.items[0].firstName;
})
let toInsert = {
name: $w(“#text22”).text, //name is the value wich I would like ti put into my database,
};
// add the item to the collection
wixData.insert(“mannequin_preferences”, toInsert). //mannequin_preference in the name of databse
. catch ( (err) => {
console.log(err);
} );
}

Thanks,

As already mentioned in your previous post from today about the same thing.
https://www.wix.com/corvid/forum/community-discussion/how-to-use-lastname-of-privatemembersdata-in-other-database?

You need to be using the current user from the Wix Users API
https://www.wix.com/corvid/reference/wix-users.html#currentUser

Not sure why you are using insert when you are just saving it to another dataset, however with your code listed above.

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

$w.onReady(function () {
//TODO: write your page related code here...
});

export function button1_click() {
// create an item
wixData.query("Members/PrivateMembersData")
.eq("_id", wixUsers.currentUser.id)
.find()
.then((results) => {
//script here
$w('#text22').text = results.items[0].firstName;
})
let toInsert = {
name: $w("#text22").text,       //name is the value wich I would like ti put into my database,
};
// add the item to the collection
wixData.insert("mannequin_preferences", toInsert).   //mannequin_preference in the name of databse
.catch( (err) => {
console.log(err);
} );
}

You need to change it to this for it to work and show the site members first name in a user input box and insert it into the field in your other dataset.

You need to add the current user lines and to remove the closing curly bracket and parentheses before the insert function and add it after the insert, otherwise it will not run and not insert the value into the dataset.

Just note that in my code I have not added the ‘-’ in your dataset, you will need to add it if you wish to keep using your current dataset, along with using the field name and key of First Name (firstName) instaed of Name (name) as you have used.

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;

export function button1_click() {
wixData.query("Members/PrivateMembersData")
.eq("_id", wixUsers.currentUser.id)
.find()
.then((results) => {
$w('#text22').value = results.items[0].firstName;

let toInsert = {
firstName: $w("#text22").value
};

wixData.insert("mannequinpreferences", toInsert)
.then( (results) => {
let item = results;
})
.catch( (err) => {
let errorMsg = err;
});
})
}

Thank for your answer but your code doesn’t work.
I add the - missing
in ‘mannequinpreferences’ the column the field name is ‘name’ so

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;
export function button1_click() {
wixData.query("Members/PrivateMembersData")
.eq("_id", wixUsers.currentUser.id)
.find()
.then((results) => {
$w('#text22').text = results.items[0].firstName;
let toInsert = {
name: $w("#text22").text
};
wixData.insert("mannequin_preferences", toInsert)
.then( (results) => {
let item = results;
})
.catch( (err) => {
let errorMsg = err;
});
})
}

Yes that is because you are still using text and not value, you had the correct way in your original post text - Hello, I would like how can I put a value in my database.

It works perfectly for me here, just signup with any made up email address and password, then simply click on the button and your first name will appear in the top box with the same value being shown in the table as well which is taking its data from what has been added to the dataset itself.
https://givemeawhisky.wixsite.com/inserttest

I paste your code in my page and add a input1 but the name don’t appear inside.

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;
export function button1_click() {
wixData.query("Members/PrivateMembersData")
.eq("_id", wixUsers.currentUser.id)
.find()
.then((results) => {
$w('#input1').value = results.items[0].firstName;
let toInsert = {
name: $w("#input1").value
};
wixData.insert("mannequinpreferences", toInsert)
.then( (results) => {
let item = results;
})
.catch( (err) => {
let errorMsg = err;
});
})
}

Did you still need to put the underscore in your dataset name here?

wixData.insert("mannequinpreferences", toInsert)
//to this//
wixData.insert("mannequin_preferences", toInsert)

Also note that this does work in preview as when you view it through the preview you will be logged in as Admin.

On the actual live website, like my test website linked previously, you need to be logged in yourself for it to work on the page.

Hence why I said to you to add anything for the email address and password so that you can test the page after signing up and logging in and it should work.

I’ve just signed up with this ‘test@test.com’ and ‘test’ on my example website and added the first name of ‘test’.

So just try logging in with the same credentials and it should work fine for you.
Or you can make up your own if you want and try it with other names.

You will also need to make sure that any user has an actual entry in the first name field of their My Account page as shown here.

If you have just used the default Wix signup window then only the email address and password will be collected, so this field in the Wix Members app collection will be blank and return no value.

Also with above, if the new site member does not add anything to their My Account page as shown above, then this field in the Wix Members app collection will stay blank until they add an input themselves.

If you use the Wix custom window or your own Corvid signup lightbox, then you can add other user inputs for capturing first and last name etc and then these fields will be filled when the user signs up.

Sorry if you been trying it as I have been messing around with the displaying of the name and going between table list and repeater as the table was not always refreshing on the live site.

It is all working fine now and displaying the name that gets inserted into the dataset from the users first name.

Your example works well but me it does not work and your code contains two errors. I will have made two photos to see.
I already change “mannequin_preferences”

My code is fine, it is your code that is wrong.

On your picture above you are using a user input box which has the element id of #input1

As you are using a user input element, then you need to be using it as this.

$w('#input1').value = results.items[0].firstName;
let toInsert = {
firstName: $w("#input1").value

If you want to use a text box element with the element id name of #text22 instead of a user input element, then you will need to change it to suit.

$w('#text22').text = results.items[0].firstName;
let toInsert = {
firstName: $w("#text22").text

I have redone the website with a user input box first and then the text box underneath.

So once you are logged in, it will now show the first name in the input and the text boxes.

I find the problem, in fact it’s the

export function button1_click()

When I put all the code outside the function, the lastname appear in the input1.
The button is liked the submission action maybe it’s the problem

Did you add the onClick event to the button through the properties panel for the element?

It’s work fine,
thank you very much

Just a last question, if the user change their settings how can I replace it in the database ?
Because each time the submission button create a new line, and I would like juste to replace it if the name already exist in the databse.