webflow / js-webflow-api

Node.js SDK for the Webflow Data API
https://www.npmjs.com/package/webflow-api
286 stars 93 forks source link

README updateItem Call is Wrong #87

Closed josiahbryan closed 4 months ago

josiahbryan commented 1 year ago

README shows:

// call update
const updatedItem = await webflow.updateItem({
  collectionId: "[COLLECTION ID]",
  itemId: "[ITEM ID]",
  fields,
});

It should be:

// call update
const updatedItem = await webflow.updateItem({
  collectionId: "[COLLECTION ID]",
  itemId: "[ITEM ID]",
  ...fields,
});
MartinHage commented 1 year ago

The README is actually correct. The webflow.updateItem function takes an object with the key fields which is an object of all the fields that should be updated.

bcaylor10 commented 1 year ago

It's not. I had to update this as well in order for it to work.

AndreasMaros commented 1 year ago

Do have nested objects somewhere in your code? Given an item like the following:

const item = {
  name: 'My Item',
  slug: 'my-item',
  field: 'value',
  _archive: false,
  _draft: false,
};

const fields = item;

your call effectively results in this:

const updatedItem = await webflow.updateItem({
  collectionId: "[COLLECTION ID]",
  itemId: "[ITEM ID]",
  name: 'My Item',
  slug: 'my-item',
  field: 'value',
  _archive: false,
  _draft: false,
});

which is clearly not right, you do want the item data inside an object fields:

const updatedItem = await webflow.updateItem({
  collectionId: "[COLLECTION ID]",
  itemId: "[ITEM ID]",
  fields: {
    name: 'My Item',
    slug: 'my-item',
    field: 'value',
    _archive: false,
    _draft: false,
  }
});

You can confirm this with a short look at the source; the method destructures three fields from an object parameter: https://github.com/webflow/js-webflow-api/blob/9d36bb178ec86e6d97785ccb2dcb9941c1ab2297/src/api/item.ts#L128-L142

The only way your code might be correct is if you have your actual fields nested inside another object, like the following example:

const item = {
  id: 'some-item-id',
  dirty: true,
  fields: {
    name: 'My Item',
    slug: 'my-item',
    field: 'value',
    _archive: false,
    _draft: false,
  },
};

if (item.dirty) {
  const updatedItem = await webflow.updateItem({
    collectionId: "[COLLECTION ID]",
    itemId: item.id,
    ...item,
  });
}

In this case the fields object within the item object would actually end up where updateItem() expects it. (Don't do this, however, a future API change might start reading a field called dirty and start misbehaving.)

dsinghvi commented 4 months ago

This is no longer relevant given the v2 SDK release!