0

I'm building rest api to update customer address and I get the following error

{
"message": "Notice: Undefined index: UpdateAddress in C:\\xampp\\htdocs\\yourParts\\lib\\internal\\Magento\\Framework\\Reflection\\MethodsMap.php on line 75",
"trace": "#0 C:\\xampp\\htdocs\\yourParts\\lib\\internal\\Magento\\Framework\\Reflection\\MethodsMap.php(75): Magento\\Framework\\App\\ErrorHandler->handler(8, 'Undefined index...', 'C:\\\\xampp\\\\htdocs...', 75, Array)\n#1 C:\\xampp\\htdocs\\project\\lib\\internal\\Magento\\Framework\\Webapi\\ServiceOutputProcessor.php(58): Magento\\Framework\\Reflection\\MethodsMap->getMethodReturnType('vendor_name\\\\Customer\\\\...', 'UpdateAddress')\n#2 C:\\xampp\\htdocs\\project\\app\\code\\Magento\\Webapi\\Controller\\Rest.php(308): Magento\\Framework\\Webapi\\ServiceOutputProcessor->process(true, 'vendor_name\\\\Customer\\\\...', 'UpdateAddress')\n#3 C:\\xampp\\htdocs\\project\\app\\code\\Magento\\Webapi\\Controller\\Rest.php(216): Magento\\Webapi\\Controller\\Rest->processApiRequest()\n#4 C:\\xampp\\htdocs\\project\\var\\generation\\Magento\\Webapi\\Controller\\Rest\\Interceptor.php(37): Magento\\Webapi\\Controller\\Rest->dispatch(Object(Magento\\Framework\\App\\Request\\Http))\n#5 C:\\xampp\\htdocs\\project\\lib\\internal\\Magento\\Framework\\App\\Http.php(135): Magento\\Webapi\\Controller\\Rest\\Interceptor->dispatch(Object(Magento\\Framework\\App\\Request\\Http))\n#6 C:\\xampp\\htdocs\\project\\lib\\internal\\Magento\\Framework\\App\\Bootstrap.php(258): Magento\\Framework\\App\\Http->launch()\n#7 C:\\xampp\\htdocs\\project\index.php(39): Magento\\Framework\\App\\Bootstrap->run(Object(Magento\\Framework\\App\\Http))\n#8 {main}"}

that's my webapi.xml route

<route url="/V1/customers/updateAddress/:firstName/:lastName/:company/:phoneNo/:fax/:stAddress/:city/:zipCode/:country/:addressId" method="PUT">
    <service class="Bit68\Customer\Api\Customer\CustomerApiInterface" method="UpdateAddress"/>
    <resources>
        <resource ref="anonymous"/>
    </resources>

</route>

that's my class

<?php

namespace vendor_name\Customer\Model\Customer;

use \vendor_name\Customer\Api\Customer\CustomerApiInterface;
use \Magento\Customer\Api\Data\CustomerInterface;
use \Magento\Customer\Api\AccountManagementInterface;
use \Magento\Authorization\Model\UserContextInterface;

class CustomerApi implements \Bit68\Customer\Api\Customer\CustomerApiInterface{

/**
* @var \Magento\Customer\Api\CustomerRepositoryInterface
*/
private $customerRepository;

/**
* @var \Magento\Customer\Api\AccountManagementInterface
*/
private $accountManagement;

/**
* @var \Magento\Authorization\Model\UserContextInterface
*/
private $userContext;

/**
* @var \Magento\Customer\Model\ResourceModel\AddressRepository
*/
private $addressRepository;


/**
* @param \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
* @param \Magento\Customer\Api\AccountManagementInterface $accountManagement
* @param \Magento\Authorization\Model\UserContextInterface $userContext
* @param \Magento\Customer\Model\ResourceModel\AddressRepository $addressRepository
*/
public function __construct(\Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
\Magento\Customer\Api\AccountManagementInterface $accountManagement,
\Magento\Authorization\Model\UserContextInterface $userContext,
\Magento\Customer\Model\ResourceModel\AddressRepository $addressRepository){
    $this->customerRepository = $customerRepository;
    $this->accountManagement = $accountManagement;
    $this->userContext = $userContext;
    $this->addressRepository = $addressRepository;

}




 /**
 * @api
* @param string $firstName
* @param string $lastName
* @param string $company
* @param string $phoneNo
* @param string $fax
* @param string $stAddress
* @param string $city
* @param string $zipCode
* @param string $country
* @param int $addressId
* @return bool
*/
public function updateAddress($firstName,
$lastName,
$company = null,
$phoneNo,
$fax = null,
$stAddress,
$city,
$zipCode,
$country,
$addressId){

    $data = [
        "firstName" => $firstName,
        "lastName" => $lastName,
        "company" => $company,
        "phoneNo" => $phoneNo,
        "fax" => $fax,
        "stAddress" => $stAddress,
        "city" => $city,
        "zipCode" => $zipCode,
        "country" => $country,
        "addressId" => $addressId
    ];

    $address = $this->addressRepository->getById($addressId);

    if(!empty($data) && $address){
        $address->setFirstname($firstName);
        $address->setLastname($lastName);
        $address->setCompany($company);
        $address->setTelephone($phoneNo);
        $address->setFax($fax);
        $address->setStreet(["Almaza"]);
        $address->setCity($city);
        $address->setCountryId($country); //sme issues about countryId
        $address->setPostcode($zipCode);

        $this->addressRepository->save($address);

        return true;

    }

    return false;
}


/**
*
* @api
* @return array
*/
public function getAddressId(){
    $customerId = $this->userContext->getUserId();
    $customer = $this->customerRepository->getById($customerId);
    $customerAddresses = array();
    if($customer && $this->isLoggedIn()){
        $addresses = $customer->getAddresses();
        foreach($addresses as $address){
            array_push($customerAddresses, (array)$address);
        }

    }

    return $customerAddresses;
}

/**
* @return bool
*/
public function isLoggedIn(){
    $customerId = $this->userContext->getUserId();
    $userType = $this->userContext->getUserType();
    if($userType == UserContextInterface::USER_TYPE_CUSTOMER){
        return true;
    }
    return false;
}
}




?> 

that's my interface

<?php

namespace vendor_name\Customer\Api\Customer;

interface CustomerApiInterface{


/**
* @api
* @return array
*/
public function getCustomerData();

/**
* @api
* @param string $firstName
* @param string $lastName
* @param string $company
* @param string $phoneNo
* @param string $fax
* @param string $stAddress
* @param string $city
* @param string $zipCode
* @param string $country
* @param int $addressId
* @return bool
*/
public function updateAddress($firstName,
$lastName,
$company = null,
$phoneNo,
$fax = null,
$stAddress,
$city,
$zipCode,
$country,
$addressId);

/**
* 
* @api
* @return array
*/
public function getAddressId();


}

?>

1 Answer 1

0

Please check your di.xml it should look like this

app/code/Bit68/Customer/etc/di.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Bit68\Customer\Api\Customer\CustomerApiInterface" type="Bit68\Customer\Model\Customer\CustomerApi"/>
</config>
3
  • I've already written that in di.xml Nov 27, 2018 at 13:02
  • @YomnaMansour Please share CustomerApiInterface.php in question Nov 27, 2018 at 13:04
  • I've updated the question Nov 27, 2018 at 13:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.