user11255971
user11255971

Reputation:

Can't use EJS hook in Strapi showing error TypeError: ctx.render is not a function

What is the bug?

I am trying to render a page using strapi-hook-ejs but it is not working even I followed all official documentation.

It is giving following error [2020-10-13T03:34:15.198Z] error TypeError: ctx.render is not a function at Object.page (C:\Users\noman\Desktop\TestAPI\api\home\controllers\home.js:11:20) at dispatch (C:\Users\noman\Desktop\TestAPI\node_modules\koa-router\node_modules\koa-compose\index.js:44:32) at next (C:\Users\noman\Desktop\TestAPI\node_modules\koa-router\node_modules\koa-compose\index.js:45:18) at dispatch (C:\Users\noman\Desktop\TestAPI\node_modules\koa-compose\index.js:42:32) at C:\Users\noman\Desktop\TestAPI\node_modules\strapi\lib\middlewares\router\utils\routerChecker.js:79:28 at dispatch (C:\Users\noman\Desktop\TestAPI\node_modules\koa-compose\index.js:42:32) at module.exports (C:\Users\noman\Desktop\TestAPI\node_modules\strapi-plugin-users-permissions\config\policies\permissions.js:86:9) at processTicksAndRejections (internal/process/task_queues.js:97:5) at async C:\Users\noman\Desktop\TestAPI\node_modules\strapi-utils\lib\policy.js:68:5 at async serve (C:\Users\noman\Desktop\TestAPI\node_modules\koa-static\index.js:59:5) at async C:\Users\noman\Desktop\TestAPI\node_modules\strapi\lib\middlewares\xss\index.js:26:9 [2020-10-13T03:34:15.212Z] debug GET /home/page (39 ms) 500 [2020-10-13T03:34:15.255Z] debug GET /favicon.ico (1 ms) 200

Steps to reproduce the behavior

  1. I installed strapi-hook-ejs (npm i strapi-hook-ejs --save)
  2. Create a new hook.json file in config folder. (./config/hook.json)
  3. Added this code
{
    "ejs": {
        "enabled": true,
        "layout": false,
        "viewExt": "ejs",
        "partial": true,
        "cache": false,
        "debug": true
    }
}
  1. Created views folder and a home.ejs file in it (./views/home.ejs) with this code <h1><%=title%></h1>

  2. Then I created a new controller and point a route to it.

module.exports = {
    page: async ctx => {
        return ctx.render('home', {title: 'My app title'});
    },
};
  1. Now when I am going to this route I am getting the error.

Expected behavior Should render home page

My Package.json

{
  "name": "source-api",
  "private": true,
  "version": "0.1.0",
  "description": "A Strapi application",
  "scripts": {
    "develop": "strapi develop",
    "start": "strapi start",
    "build": "strapi build",
    "strapi": "strapi"
  },
  "devDependencies": {},
  "dependencies": {
    "axios": "^0.20.0",
    "strapi": "3.1.4",
    "strapi-admin": "3.1.4",
    "strapi-connector-mongoose": "3.1.4",
    "strapi-hook-ejs": "^3.2.3",
    "strapi-plugin-content-manager": "3.1.4",
    "strapi-plugin-content-type-builder": "3.1.4",
    "strapi-plugin-email": "3.1.4",
    "strapi-plugin-upload": "3.1.4",
    "strapi-plugin-users-permissions": "3.1.4",
    "strapi-provider-upload-cloudinary": "^3.1.4",
    "strapi-utils": "3.1.4"
  },
  "author": {
    "name": "Nehal Ahmad"
  },
  "strapi": {
    "uuid": "1034d7e9-73ba-49d4-8862-0d5d62ae7008"
  },
  "engines": {
    "node": "12.x",
    "npm": "6.x"
  },
  "license": "MIT"
}

System

I have tried it on latest version of strapi.

Upvotes: 0

Views: 519

Answers (1)

Related Questions