Cumulocity @c8y/client problem in InventoryService update() method

Product/components used and version/fix level:

Cumulocity Web SDK - @c8y/client v1021.31.4 (latest)

Detailed explanation of the problem:

Hi, I am currently using the c8y client to develop a custom widget plugin for Cumulocity. In particular, I need to update a managed object, so I am using the update() method of the InventoryService (InventoryService | Cumulocity Web SDK - v1021.31.4).
This method takes only a Partial<IManagedObject> as input, but does not have any reference to the object id. I tried to include the id in the input but the request is not recognized by Cumulocity. In fact, the PUT api to update objects takes the id as path variable (inventory/managedObject/:id).

Error messages / full error message screenshot / log file:

I received a 405 error saying “Method not allowed”. I can see in logs that the endpoint called is PUT inventory/managedObject (without the id).
Is this a bug of the InventoryService or am I doing something wrong?

Question related to a free trial, or to a production (customer) instance?

Production

Hi @davidelh,

it would help if you could provide a code snippet of what you are doing.

Something like this should work:

const partialUpdateObject: Partial<IManagedObject> = {
   id: '123456'
   customFragment: 'Changed data',
   name: 'Name'
 };

 (async () => {
   const {data, res} = await inventoryService.update(partialUpdateObject);
 })();

The id is taken from the object you are passing in.
The types could be improved here as the id is a required attribute of the object you are passing in…

Regards,
Tristan

1 Like

Hi @Tristan_Bastian ,

Here is my snippet, it is very similar to yours (I started from the official documentation). I made sure that asset_id is always defined:

async updateAsset(asset_id: string) {
    try {      
      const partialUpdateObject: Partial<IManagedObject> = {
        id: asset_id,
        myProperty: 'new property value'
      };
      const {data, res} = await this.inventoryService.update(partialUpdateObject);
      console.log('Asset updated successfully');
    } catch (error) {
      console.error('Error updating asset: ', error);
    }
}

From the documentation, it does not seem that the id is a required attribute of the object (InventoryService | Cumulocity Web SDK - v1021.31.4).

Here is a screen capture of the error log:

As you can see, no id is included in the url path.

Hi @davidelh,

Only guess I have is, that you are not actually passing an asset_id when calling updateAsset.
Can you log the partialUpdateObject object before sending it? Like e.g. this:

async updateAsset(asset_id: string) {
    try {      
      const partialUpdateObject: Partial<IManagedObject> = {
        id: asset_id,
        myProperty: 'new property value'
      };
      console.log(JSON.stringify(partialUpdateObject));
      const {data, res} = await this.inventoryService.update(partialUpdateObject);
      console.log('Asset updated successfully');
    } catch (error) {
      console.error('Error updating asset: ', error);
    }
}

Regards,
Tristan

Hi @Tristan_Bastian ,

Yes, it was a problem with the asset_id that for some reason was passed as undefined to the updateAsset method. Now I can correctly see the id in the partialUpdateObject and the update() method works.

Thank you for your help!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.