Reputation: 11
Trying to use contentfil entry with API but getting some error TypeError: client.getEntry is not a function . Everything seems good, googled a lot but did not find any answer.
const contentful = require('contentful-management')
const client = contentful.createClient({
space: 'sdfdrf65435',
accessToken: 'sdf43tgfhg34tgdfgdsf',
host: 'preview.contentful.com'
})
console.log("client---->",client)
client.getEntry('sdf34ythrgfgd')
.then((entry) => console.log(entry))
.catch(console.error)
This is the code I tried, but not working somehow
Upvotes: 0
Views: 574
Reputation: 61
I had the same error because I was using the plain API which is better than the typical client (which you are using) as per as the documentation on npm. with plain client you need to use entry.get function:
//first we create a client with our defaults:
const scopedPlainClient = contentful.createClient(
{
// This is the access token for this space. Normally you get the token in the Contentful web app
accessToken: //your CMA access token,
},
{
type: 'plain',
defaults: {
spaceId: //your space id,
environmentId://your env id typically: 'master',
},
}
)
Now, a direct call of getEntry won't work:
//this won't work
await scopedPlainClient.getEntry(<entry_id>)
//this will work
copedPlainClient.entry.get({
entryId: <entry_id>
})
Upvotes: 0
Reputation: 191
It looks like you're using the Management SDK. Taking a quick look at the documentation, you need to do something as follow:
const contentful = require('contentful-management')
const client = contentful.createClient({
accessToken: '<content_management_api_key>'
})
client.getSpace('<space_id>')
.then((space) => space.getEnvironment('<environment_id>'))
.then((environment) => environment.getEntry('<entry_id>'))
.then((entry) => console.log(entry))
.catch(console.error)
If you just want to fetch content and not manipulate it from the client side, you should use the Delivery API.
Upvotes: 0