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.
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.
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).
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.