I’m trying to input the logged member ID into a CMS
in Wix
. I can’t find a way to do this without using code, and when I try to use code, it works but creates an additional item in the database just for the member ID insert.
I believe this happens because the other data I send im using the no-code method connecting the inputs to the specific table from the database for every input on the screen. The only way I found to insert the current logged member ID into the CMS
is through JavaScript, but it’s sent twice: once via the no-code method, sending all the data and once via code sending only the memberId
. I can’t find a way to ensure everything is sent only once without creating 2 items in the database.
here is my code to send the memberId
:
export function buttonSubmit_click(event) {
currentMember.getMember()
.then(member => {
if (member) {
const userId = member._id;
const toInsert = { "userId": userId };
wixData.insert("Empresas1", toInsert)
.then(result => {
console.log("Item inserido com sucesso:", result);
})
.catch(err => {
console.error("Erro ao inserir o item:", err);
});
} else {
console.log("Nenhum membro logado.");
}
})
.catch(err => {
console.error("Erro ao obter o membro:", err);
});
}
There are probably a few ways to go about this. But first, do you really need to do anything? If a member is logged in when the no-code connections insert an item into a collection, there should be an Owner (_owner
) field with the user ID. This field is hidden by default and can be shown using the Manage Fields button.
If that doesn’t do what you want, and you need to programmatically add the user ID, you should use the wix-dataset
API instead of wix-data
. You can use the setFieldValue
behind the scenes to add the user ID to the current item. When the user submits the rest of the data, the user ID you set pragmatically will be submitted as well.