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.
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));
}, []);