This is a node.js question.
I am trying to use the contentful API which is a CMS service.
This works:
const contentful = require("contentful");
const client = contentful.createClient({
space: SPACE_ID,
accessToken: ACCESS_TOKEN
})
client.getEntry("27xsh9l8AWraqFQwICeaCn").then((doc)=>{
console.log(doc);
});
However when I use the exact same code like this:
A.js
module.exports = class Contentful{
constructor(){
this.produceTemplate();
}
async produceTemplate() {
console.log("Fetching Contentful");
var doc = await client.getEntry("27xsh9l8AWraqFQwICeaCn")
console.log("done");
}
}
B.js
class Interpreter {
constructor(user) {
let contentful = new Contentful();
// This essentially waits until the contentful object successfully fetches the data and allocates it to a variable called RootTemplate
if(contentful.RootTemplate == null || contentful.RootTemplate == undefined){
console.log("Waiting for Contentful service..");
while(contentful.RootTemplate == null || contentful.RootTemplate == undefined){}
}
}
}
When I run the code output is
Fetching Contentful
So the promise
var doc = await client.getEntry("27xsh9l8AWraqFQwICeaCn")
never resolves. It gots stock.
Why is this happening? Is the class blocking me? or is there something wrong with the "contentful" library?
Note: I construct the Interpreter from my index.js, I did not put it because I think it is irrelevant Note2: I create a client object in A.js with exact same space_ID and Access_Token. I cut them out while posting here in order to make code more readeble