using wixCrm.getContactById return an error

hello every one!

i’m trying to get the client first name, last name and email using
wixCrm.getContactById
here is my cod:

let firstName = “”;
let lastName = “”;
let id = context.userId;
let email = “”;
console.log(context.userId);
let contanct = await wixCrm.getContactById(context.userId);
console.log(contanct);
item.firstName = contanct.firstName;
item.lastName = contanct.lastName;
item.title = id;
item.email = contanct.loginEmail;
return item;

the problem is that I don’t get anything.

I tried to use this function in the page code instead of the backend and i’m getting this error:

TypeError: _wixCrmBackend2.default is undefined

Unhandled promise rejection TypeError: _wixCrmBackend2.default is undefined

thanks in advance!

Because to use getContactById you need to use the wix-crm-backend as shown:

import wixCrm from 'wix-crm-backend';

export function myBackendFunction(contactId) {
  return wixCrm.getContactById(contactId);
}

See here:

It is only possible to get limited information from the CRM such as email and userId. It is not possible to read all the fields in the CRM though Wix code, you can’t even get the persons name from the CRM.

This is what you can get:

See here:
https://www.wix.com/code/reference/wix-users.html#currentUser

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"
  } );

user.getRoles()
  .then( (roles) => {
    let firstRole = roles[0];
    let roleName = firstRole.name;                // "Role Name"
    let roleDescription = firstRole.description;  // "Role Description"
  } );

user.getPricingPlans()
  .then( (pricingPlans) => {
    let firstPlan = pricingPlans[0];
    let planName = firstPlan.name;          // "Gold"
    let startDate = firstPlan.startDate;    // Wed Aug 29 2018 09:39:41 GMT-0500 (Eastern Standard Time)
    let expiryDate = firstPlan.expiryDate;  // Thu Nov 29 2018 08:39:41 GMT-0400 (Eastern Daylight Time)
  } );

The APIs in wix-users are only partially functional when previewing your site. View a published version of your site to see their complete functionality.

The APIs in wix-users can only be used once the page has loaded. Therefore, you must use them in code that is contained in or is called from the onReady() event handler or any element event handler.

See here:
https://www.wix.com/code/reference/wix-users-backend.html#currentUser

import wixUsers from 'wix-users-backend';

// ...

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"
  } );

user.getRoles()
  .then( (roles) => {
    let firstRole = roles[0];
    let roleName = firstRole.name;                // "Role Name"
    let roleDescription = firstRole.description;  // "Role Description"
  } );

To actually do what you are requesting will involve creating another dataset similar to Wix CRM and using that instead, or you can use existing datasets like ‘Members’ if you have member profiles setup on your website for your existing site members (although note that your Members dataset might be called a different name).

thanks!

but my code is in the backend already, it is in before_insert hook.
if i’m trying this code:

export async function mydataBase_beforeInsert(item, context) {

let contanct = await myBackendFunction(context.userId);

item.email = await contanct.getmail();
console.log(item.email)
return item;
}

export async function myBackendFunction(contactId) {
console.log(“inFunc”)
return await wixCrm.getContactById(contactId);
}

it won’t give me the mail

i also tried to use a query on the PrivateMembersData which also didn’t work.

The dataset hooks are setup in the backend as data.js files, I use them myself. The before insert hook will alter the input into the dataset before being saved, so in theory it will do whatever you code it to do, as like some of mine which are setup bascially to make sure that user inputted names are always upper and lower case regardless of how they typed them into my form.

However you still need a backend function for the wix-crm-backend function, the data.js file isn’t able to call the backend function for the wix-crm as you currently have no other backend file.

mydataBase - is this your dataset id name?
myBackendFunction - this should be the name of your backend function within your backend code.

mydataBase is my collection.

so where i should create the backend function?

You need to open Backend in the site structure sidebar in your editor and add a new file, then you can add your backend code to this new file.