I am struggling to get the JavaScript Contentful SDK working within my Nativescript application. Sadly, I am new to NativeScript so may be missing something fundamental. Hopefully someone can help :)
The module is installed in my NativeScript project in the usual way:
npm install contentful
I then make a simple module (based on an example I found at Github https://github.com/contentful-labs/product-catalogue-web.ts):
import {ContentfulClientApi, createClient} from 'contentful';
export class ContentfulService {
cdaClient: ContentfulClientApi;
constructor() {
this.cdaClient = createClient({
space: 'spaceId',
accessToken: 'token'
});
}
getContent() {
this.cdaClient.getContentTypes()
.then(function(response) {
console.log(response.items);
return response.items;
})
}
}
And in a NativeSctipt view model:
import { ContentfulService } from "../shared/cms";
let cms = new ContentfulService();
cms.getContent();
... but nothing happens. No errors or debug output, however the Native Script view model doesn't do anything at all - code doesn't execute. If I remove the createClient()
code from the constructor and I just put a console.log in getContent()
then the console.log works as expected when I call getContent() from the view model
If I try this outside of NativeScript e.g. using the boiler plate node project from the Contentful dashboard, that works as expected.
The reason I want to use the SDK is I need to support offline content via the sync, and the SDK seems to make this easy (if I could get it working!).
Thanks for any help
So it seems that the Contentful SDK isn't compatible with Native Script, so for now I'm going to swap to using React Native. You can use fetch with the Contentful REST API but I was after some of the extra SDK functionality like offline caching.