Deleting a member

Question:
hello everyone,

I have a little problem, which, I suppose can be solved here.
I have a page where I can add new contacts. That works great, no pb.
The pb is that they are automatically save as members also. I don’t want that : only a contact NOT a member.

1/ is there a way to save them only as a contact? (Note as member)
2/ How to delete this member in velo code?

Product:
Wix Editor

**What have you already tried:

import { authentication } from ‘wix-members-frontend’;
import {session} from ‘wix-storage’;
import wixData from ‘wix-data’;
import wixUsers from ‘wix-users’;

let emails = ;

$w(‘#Register’).onClick((event) => {

// Vérifier si les MDP concordent.
if($w(‘#CfrmPassword’).value !== $w(‘#Password’).value){
console.log(‘Les MDP ne concordent pas’);
$w(‘#Recap’).show();
$w(‘#Recap’).html = `

Attention, vos mots de passe sont différents.Merci de vérifier votre saisie…

-

} else {
console.log(‘les MDP concordent bien’);
$w(‘#Recap’).show();
$w(‘#Recap’).html = `

Enregistrement de vos informations en cours. Merci de patienter, vous serez automatiquement redirigé vers le site …

-
$w(‘#Register’).show()

const password = $w(‘#Password’).value;
const email = $w(‘#Email’).value;
emails.push(email);

let options = {
contactInfo: {
firstName: $w(‘#PrenomClt’).value,
lastName: $w(‘#NomClt’).value,
emails: emails,
},
privacyStatus: ‘PUBLIC’
}

authentication.register(email, password, options)
.then((registrationResult) => {
console.log(‘L enregistrement du nouveau client a bien été validé.:’, registrationResult);
console.log('Email client : ', email);

session.setItem(‘MailClt’, $w(‘#Email’).value);
console.log($w(‘#Email’).value)

})
.catch((error) => {
console.error(error);
});
}

});

thank you very much for your help

Hi, @user3410 !!

Hmm :thinking:, I see. I’m not sure if I fully understand what you’re aiming for, but if you just want to collect “contact information,” I think simply using a “form” element would be enough—you don’t need any code. For example, you can set up a form with input fields for things like name and email address. When users fill out the required fields and click the submit button, they should be registered as a contact. You can check whether they were successfully added from the dashboard. :innocent:

hello

What about this code :

$w(‘#button44’).onClick((event) => {

const contactInfo = {
name: {
first: $w(‘#firstName’).value,
last:$w(‘#lastName’).value,
},
emails: [{
email: $w(‘#email’).value
}]

};

contacts.appendOrCreateContact(contactInfo)
.then((resolvedContact) => {
if (resolvedContact){
displayMessage(“AJOUT OK : Nouveau client enregistré”)
$w(‘#Messagetext’).show()
}else{
displayMessage(“ERREUR : Client n’a pas pu être enregistré”)
$w(‘#Messagetext’).show()
}
console.log(resolvedContact)
})

  .catch((error) => {
    console.error(error);
  });    

})

Hi onemoretime

First of all, thanks for your help.

Of course, this is a solution : my customers still use it on the page 'Reservation" : their informations are directly saved in the contacts collection.

But when I use it on my page (where nobody can cannect apart of me : this is a page wher i create contacts/reservations myself), they are not saved in the contacts collection. I dont see them? For sure the submisson form has bee saved because i View the records in the submissions table, but not in contact list? Why?

So that is why i would like to code this => Adding new contact but ONLY A CONTACT, not a member.

The code seems to be Ok but nothing in wix contact is save…
Maybe permissions?

The code i Use:

$w.onReady(function () {

$w(‘#EnregistrerContact’).onClick(async (event) => {
console.log(“Test Contact”);
const contactInfo = {
name: {
first: $w(‘#prenomclt’).value,
last: $w(‘#nomclt’).value
},
emails: [{
email:$w(‘#mailclt’).value
}],
phones: [{
tag: “PORTABLE”,
countryCode: “FR”,
phone: $w(‘#input9’).value
}],
labelKeys: [
“Test ajout nouveau contact”
]
};

  try {
  	await contacts.appendOrCreateContact(contactInfo);
  	console.error(contactInfo)
  	console.log("Contact created with ID:", contactInfo.contactId);
  	$w('#IDClient').value=contactInfo.contactId
    } catch (error) {
        console.error(error);
    }

});
});

here the logs:

Hmm :thinking:, I’m not entirely sure, but according to the reference for the API you’re currently using (appendOrCreateContact), its behavior may vary depending on the state of the user being processed.

So I was thinking, maybe you could try rewriting it using this backend-based API instead. :upside_down_face:

yes i tried it also.
But:
1/ “wix-web-module” is not recognized? Do i have to create a backend code?
2/error with “Createcontac”

here the captures:

image

I believe this is a backend-only API, so it needs to be used within backend code. Try placing the function in the backend and then calling it from the frontend. :smiling_face_with_sunglasses:

The error you’re seeing with the web module is likely because you’re importing it in the frontend. The web module is meant to be used in the backend and called from the frontend, so it must be imported within backend code. :upside_down_face:

The same goes for the error with createContact—it’s happening for the same reason. In short, you’re importing APIs intended for backend use inside frontend code, which is why you’re getting these errors. :smiling_face_with_sunglasses:

To explain in more detail: first, you need to create a web.js file in the backend and write code like the example below inside it. Then, you need to call and use that backend function from the frontend, as shown in the example below.

// this example is cited from "https://dev.wix.com/docs/velo/apis/wix-crm-backend/contacts/create-contact"

// sample.web.js in backend

import { Permissions, webMethod } from "wix-web-module";
import { contacts } from "wix-crm-backend";

export const myCreateContactFunction = webMethod(Permissions.Anyone, () => {
  const contactInfo = {
    name: {
      first: "Ari",
      last: "Thereyet",
    },
  };

  const options = {
    allowDuplicates: false,
    suppressAuth: false,
  };

  return contacts
    .createContact(contactInfo, options)
    .then((contact) => {
      return contact;
    })
    .catch((error) => {
      console.error(error);
    });
});

then…

// in frontend

import { myCreateContactFunction } from "backend/sample.web.js";

$w.onReady(function () {

    myCreateContactFunction();

});

It can be frustrating when errors appear while you’re writing code, but in fact, those errors are actually quite helpful. They’re the editor’s way of telling you exactly what needs to be fixed—and if you follow the guidance, you’ll definitely be able to solve them.

When you’re facing clear, well-defined errors like this, taking it step by step will likely lead to success. So try to approach it with a lighter mindset—it might go more smoothly than you think. :innocent:

If this code runs successfully and adds “Thereyet Ari” to your contacts correctly, then you’re almost there—just a few more changes to the code should make everything work. Specifically, you’ll need to update it so that the frontend passes your custom contact-related arguments to the backend for processing. :upside_down_face:

1 Like

Thank you so much onemoretime, for taking the time to look into my problem and explain it to me. Indeed, I’m learning every day thanks to this forum and some active members like you. Thanks again. :grinning_face:

It’s Ok, i put the code in the backend (no error anymore, and call it from the frontend : GREAT!!

Last question : As you mentioned, I now need to create the link between my backend and my frontend. :thinking:

Currently, it creates a contact with a well-defined name (“Ari Thereyet”, phones, etc…)

I would now like to be able to use the variables contained in my fields in the frontend and assign them to each field (name, phone number, etc.) declared in the backend?

Do I need to create a function?

Thank you again :ok_hand:

Here the code from backend:

> import { contacts } from 'wix-crm-backend';
> import { Permissions, webMethod } from "wix-web-module";
> 
> export const myCreateContactFunction = webMethod(Permissions.Anyone, (Prenom, Nom,Email) => {
> 
>   const contactInfo = {
>     name: {
>       first: Prenom,
>       last: Nom
>     },
>     emails: [
>       {
>         //
>         email: Email,
>         primary: true,
>         tag: "MAIN",
>       },
>     ],
>     
>   };
> 
>   const options = {
>     allowDuplicates: false,
>     suppressAuth: true,
>   };
> 
>   return contacts
>     .createContact(contactInfo, options)
>     .then((contact) => {
>       console.log("Contact enregistré");
>       return contact;
>       
>     })
>     .catch((error) => {
>       console.error(error);
>     });
> });

And my frontend: (With the variables Prenom, Nom & Email):

> $w('#EnregistrerContact').onClick((event) => {
> 
> 	const Prenom = $w("#input3").value;
>   	const Nom = $w("#input2").value;
> 	const Email = $w("#input8").value;
> 
>     myCreateContactFunction(Prenom,Nom,Email);
> 
> })

I’m glad to hear that it worked! :grin: As you can see, at this point, the contactInfo is currently set as a fixed value within the backend code. This is just to demonstrate an example implementation and isn’t something you would typically do in a real scenario. So the goal is to make the contactInfo dynamic. :thinking:

Simply put, you would create an object like this on the frontend and pass it as an argument to myCreateContactFunction. If we consider the simplest scenario without any validation to check whether the values are acceptable, it should work by doing something like this.

// in frontend

import { myCreateContactFunction } from "backend/sample.web.js";

$w.onReady(function () {

  const contactInfo = {
    name: {
      first: $w("#firstName").value,
      last: $w("#lastName").value,
    },
  };

  myCreateContactFunction(contactInfo);

});


// in backend

import { Permissions, webMethod } from "wix-web-module";
import { contacts } from "wix-crm-backend";

export const myCreateContactFunction = webMethod(Permissions.Anyone, (contactInfo) => {

  const options = {
    allowDuplicates: false,
    suppressAuth: false,
  };

  return contacts
    .createContact(contactInfo, options)
    .then((contact) => {
      return contact;
    })
    .catch((error) => {
      console.error(error);
    });

The basic idea of passing data works as shown here, but as it is, this code lacks many important aspects, such as input validation (what values are accepted or rejected), security considerations, and proper error handling. It might be a good idea to ask an AI like ChatGPT how to refine the code to make it more secure. AI is particularly good at applying standardized best practices, so you can generally trust it to guide you in the right direction. Also, if this function is only meant to be used by you or other administrators, there’s no need to set the Permissions to Anyone for now.

1 Like

All works perfectly with your help.
My code above is ok : just the refresh takes a few minutes but no problem :slight_smile:

I will take a look for the security as you told me.

See you soon

Thanks a lot.

1 Like