I had created a custom API for customer registration in Magento 2.3. I am checking that the weather the customer is already registered or not with below code.
$email = $this->request->getParam('email');
$firstName = $this->request->getParam('firstname');
$lastName = $this->request->getParam('lastname');
$websiteId = $this->request->getParam('websiteId');
$customerFactory = $this->_customerFactory->getCollection();
/**
* check whether the email address is already registered or not
*/
$customer = $customerFactory->setWebsiteId($websiteId)->loadByEmail($email);
/**
* if email address already registered, return the error message
* else, create new customer account
*/
if ($customer->getId()) {
echo 'Customer with email '.$email.' is already registered.';
} else {
// code...
}
But I am getting the api error when i run in postman. When I checked the api log report it shows below error.
"Fatal Error: 'Uncaught Error: Call to undefined method Magento\\Customer\\Model\\ResourceModel\\Customer\\Collection\\Interceptor::setWebsiteId() in \/var\/www\/html\/app\/code\/MyModule\/Customapi\/Model\/AppcustomerregistrationManagement.php:187\nStack trace:\n#0 [internal function]: MyModule\\Customapi\\Model\\AppcustomerregistrationManagement->signup()\n#1 \/var\/www\/html\/vendor\/magento\/module-webapi\/Controller\/Rest\/SynchronousRequestProcessor.php(95): call_user_func_array(Array, Array)\n#2 \/var\/www\/html\/vendor\/magento\/module-webapi\/Controller\/Rest.php(188): Magento\\Webapi\\Controller\\Rest\\SynchronousRequestProcessor->process(Object(Magento\\Framework\\Webapi\\Rest\\Request\\Proxy))\n#3 \/var\/www\/html\/vendor\/magento\/framework\/Interception\/Interceptor.php(58): Magento\\Webapi\\Controller\\Rest->dispatch(Object(Magento\\Framework\\App\\Request\\Http))\n#4 \/var\/www\/html\/vendor\/magento\/framework\/Interception\/Interceptor.php(138): Magento\\Webapi\\Controller\\Rest\\Interceptor->___callParent('dispatch', Array)\n#5 \/var\/www\/html\/vendor\/magento\/framework\/Intercept' in '\/var\/www\/html\/app\/code\/MyModule\/Customapi\/Model\/AppcustomerregistrationManagement.php' on line 187"