ERROR : ││ TypeError: Error creating endpoint POST /api/generate-backend-function: Cannot read properties of undefined (reading 'generateBackendFunction')

actually im tring create a plugin that serve to strapi community to have ready backend post request function of the content type
src/server/code-generation/generateBackendFunction.js : // code-generation/generationBackEndFunction.js
‘use strict’;

// Import required modules
const Mustache = require(‘mustache’);

// Define the generateBackendFunction function
async function generateBackendCode(model, method) {
// Sample model schema
const modelSchema = {
kind: ‘collectionType’,
collectionName: ‘blogs’,
attributes: {
Title: { type: ‘string’, required: true, maxLength: 20 },
Description: { type: ‘string’, required: true },
image: { type: ‘string’, required: true },
Sujet: { type: ‘string’ },
},
};

// Sample HTTP request type
const requestType = 'POST';

// Define a template for the backend function
const template = `
    module.exports = {
        async handle{{RequestType}}Request(ctx) {
            const { {{Attributes}} } = ctx.request.body;
            try {
                // Add your business logic here
                // e.g., create a new blog entry in the database
                const newBlog = await strapi.services.blog.create({ {{Attributes}} });
                ctx.send(newBlog);
            } catch (error) {
                ctx.throw(500, 'Error handling {{RequestType}} request', { error });
            }
        },
    };
`;

// Extract attribute names from the model schema
const attributes = Object.keys(modelSchema.attributes);

// Render the template with the extracted attributes and request type
const renderedCode = Mustache.render(template, {
    RequestType: requestType.toUpperCase(),
    Attributes: attributes.join(', '),
});

// Return the rendered code
return renderedCode;

}

// Export the generateBackendFunction function
module.exports = generateBackendCode;
// [ src/server/controllers/GenerateBackendFuntion.js : ‘use strict’;

const generateBackendCode = require(‘…/code-generation/generateBackendFunction’); // Corrected import

module.exports = {
async generateBackendFunction(ctx) {
try {

        const { model, method } = ctx.request.body;
        const backendCode = await generateBackendCode(model, method);
        ctx.send({ backendCode });
    } catch (error) {
        console.error('Error generating backend code:', error);
        ctx.throw(500, 'Internal server error');
    }
}

};
// src/server/routes/index.js : {
method: “POST”,
path: “/api/generate-backend-function”,
handler: “GenerateBackendFunction.generateBackendFunction”,
config: {
auth: false
}

}
but when i run " npm run develop " i get this error : ⠦ Loading Strapi[ERROR] There seems to be an unexpected error, try again with --debug for more information

┌─────────────────────────────────────────────────────────────────────────────────────────────────┐│ ││ TypeError: Error creating endpoint POST /api/generate-backend-function: Cannot read ││ properties of undefined (reading ‘generateBackendFunction’) ││ at getAction (C:\Users\USER\Downloads\StrapiGen=master 1\StrapiGen=master\StrapiGEN\node_mo ││ dules@strapi\strapi\dist\services\server\compose-endpoint.js:104:24) ││ at C:\Users\USER\Downloads\StrapiGen=master 1\StrapiGen=master\StrapiGEN\node_modules@stra ││ pi\strapi\dist\services\server\compose-endpoint.js:56:22 ││ at createRoute (C:\Users\USER\Downloads\StrapiGen=master 1\StrapiGen=master\StrapiGEN\node_ ││ modules@strapi\strapi\dist\services\server\routing.js:71:5) ││ at C:\Users\USER\Downloads\StrapiGen=master 1\StrapiGen=master\StrapiGEN\node_modules@stra ││ pi\strapi\dist\services\server\routing.js:80:9 ││ at Array.forEach () ││ at Object.addRoutes (C:\Users\USER\Downloads\StrapiGen=master 1\StrapiGen=master\StrapiGEN\ ││ node_modules@strapi\strapi\dist\services\server\routing.js:78:21) ││ at Object.routes (C:\Users\USER\Downloads\StrapiGen=master ││ 1\StrapiGen=master\StrapiGEN\node_modules@strapi\strapi\dist\services\server\api.js:20:20) ││ at Object.routes (C:\Users\USER\Downloads\StrapiGen=master 1\StrapiGen=master\StrapiGEN\nod ││ e_modules@strapi\strapi\dist\services\server\index.js:52:27) ││ at registerPluginRoutes (C:\Users\USER\Downloads\StrapiGen=master 1\StrapiGen=master\Strapi ││ GEN\node_modules@strapi\strapi\dist\services\server\register-routes.js:43:21) ││ at registerAllRoutes (C:\Users\USER\Downloads\StrapiGen=master 1\StrapiGen=master\StrapiGEN ││ \node_modules@strapi\strapi\dist\services\server\register-routes.js:20:3) ││

Hi @Moataz_MHAMDI,

I was getting a similar error as well.

 TypeError: Error creating endpoint GET /product-features: Cannot read properties of undefined (reading         ││   'find')

When I checked on my source files. This file was removed

backend/src/api/product-feature/routes/product-feature.js

After restoring the file everything was back to normal. In a nutshell, ensure the route you are trying to access exists under your routes. Also, ensure you run npm run develop on a stable internet to avoid Error: ETIMEDOUT: connection timed out, write.

For me the reason was that the controller name had an uppercase letter. Turns out, you can’t use uppercase in controller’s file name and in route file where you specify controller name in handler property