Closed josiahbryan closed 4 months 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.
It's not. I had to update this as well in order for it to work.
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.)
This is no longer relevant given the v2
SDK release!
README shows:
It should be: