Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,004 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
The Below function app code should execute while a webhook is triggered. The below code runs fine. But eventhough the webhook is triggered once. The below code runs indefinitely. Is anything is wrong in the below code which cause the issue.
**Index.js**
module.exports = async (context, req) => {
context.log("JavaScript HTTP trigger function processed a request.");
const contentful = require("contentful-management");
const client = contentful.createClient({
accessToken: "test",
});
const spaceId = "test";
const environmentId = "test";
try {
const space = await client.getSpace(spaceId);
const environment = await space.getEnvironment(environmentId);
let response = await environment.getEntry(context.req.body.sys.id);
const firstEntry = response;
context.log(firstEntry);
if (
firstEntry.fields.previousSlug != undefined &&
firstEntry.fields.slug != firstEntry.fields.previousSlug
) {
const testingContentTypeId = "redirectManager"; // Replace with the actual content type ID
const testingEntry = await environment.createEntry(testingContentTypeId, {
fields: {
id: { "en-GB": firstEntry.fields.internalDocumentId["en-GB"] },
redirectSlug: {
"en-GB": firstEntry.fields.previousSlug["en-GB"],
},
currentSlug: { "en-GB": firstEntry.fields.slug["en-GB"] },
},
});
testingEntry.publish();
}
firstEntry.fields.previousSlug = firstEntry.fields.slug;
var result = await firstEntry.update();
await result.publish();
} catch (error) {
context.res = {
body: error,
};
return "test";
};
};
**Function.json:**
{
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "res"
}
]
}
Hello @Ramji R - Thanks for reaching out. The bindings look OK, and the function signature and the return also look OK to me. However, I do have the following questions for you which might help troubleshoot the root cause:
const contentful = require("contentful-management");
inside the function (instead of being at the top) work as intended and the client object is created successfully? Note that require()
is a synchronous call and can block the rest of the execution.Please "Accept Answer" if the answer is helpful so that others in the community may benefit from your experience.