Contentful - Next.js Versioning Issue Error 409

310 views Asked by At

I'm trying to update an existing entry in Contentful with the Contentful Management API.

In Contentful, every entry included a views field which represents a views counter. I want to increase that counter every time the page is reloaded. But I'm running into a versionMismatch error.

VersionMismatch

Here's the source code:

const [ViewsCounter, setViewsCounter] = useState(views);

useEffect(() => {
  client
    .getSpace("------")
    .then((space) => space.getEnvironment("master"))
    .then((environment) => environment.getEntry(PostId))
    .then((entry) => {
      entry.fields.views["en-US"] = ViewsCounter;
      return entry.update();
    })
    .then((entry) => entry.publish())
    .catch(console.error);
}, [ViewsCounter]);

useEffect(() => {
  setViewsCounter((prev) => (prev += 1));
}, []);
0

There are 0 answers