Giter Club home page Giter Club logo

Comments (4)

MartinHage avatar MartinHage commented on May 24, 2024

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.

from js-webflow-api.

bcaylor10 avatar bcaylor10 commented on May 24, 2024

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

from js-webflow-api.

AndreasMaros avatar AndreasMaros commented on May 24, 2024

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:

static update(
{
collectionId,
itemId,
fields,
}: {
fields: any;
itemId: string;
collectionId: string;
},
client: AxiosInstance,
) {
requireArgs({ collectionId, itemId });
const path = `/collections/${collectionId}/items/${itemId}`;
return client.put<IItem>(path, { fields });

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.)

from js-webflow-api.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.