首页 > 解决方案 > 无法在 Strapi 中使用 EJS 挂钩显示错误 TypeError: ctx.render is not a function

问题描述

什么是错误?

我正在尝试使用strapi-hook-ejs 呈现页面,但即使我遵循了所有官方文档,它也无法正常工作。

它给出以下错误 [2020-10-13T03:34:15.198Z] 错误 TypeError: ctx.render is not a function at Object.page (C:\Users\noman\Desktop\TestAPI\api\home\controllers\home .js:11:20) 在调度 (C:\Users\noman\Desktop\TestAPI\node_modules\koa-router\node_modules\koa-compose\index.js:44:32) 在下一个 (C:\Users\noman \Desktop\TestAPI\node_modules\koa-router\node_modules\koa-compose\index.js:45:18) 在调度 (C:\Users\noman\Desktop\TestAPI\node_modules\koa-compose\index.js:42 :32) 在 C:\Users\noman\Desktop\TestAPI\node_modules\strapi\lib\middlewares\router\utils\routerChecker.js:79:28 在调度 (C:\Users\noman\Desktop\TestAPI\node_modules\ koa-compose\index.js:42:32) 在 module.exports (C:\Users\noman\Desktop\TestAPI\node_modules\strapi-plugin-users-permissions\config\policies\permissions.js:86:9) 在 processTicksAndRejections (internal/process/task_queues.js:97:5) 在异步 C:\Users\noman\Desktop\TestAPI\node_modules\strapi-utils\lib\policy.js:68:5 在异步服务 (C :\Users\noman\Desktop\TestAPI\node_modules\koa-static\index.js:59:5) 在异步 C:\Users\noman\Desktop\TestAPI\node_modules\strapi\lib\middlewares\xss\index.js :26:9 [2020-10-13T03:34:15.212Z] 调试 GET /home/page (39 ms) 500 [2020-10-13T03:34:15.255Z] 调试 GET /favicon.ico (1 ms) 200

重现行为的步骤

  1. 我安装了strapi-hook-ejs(npm我strapi-hook-ejs --save)
  2. 在 config 文件夹中创建一个新的 hook.json 文件。(./config/hook.json)
  3. 添加了此代码
{
    "ejs": {
        "enabled": true,
        "layout": false,
        "viewExt": "ejs",
        "partial": true,
        "cache": false,
        "debug": true
    }
}
  1. 使用此代码在其中创建了 views 文件夹和 home.ejs 文件(./views/home.ejs)<h1><%=title%></h1>

  2. 然后我创建了一个新控制器并指向它的路由。

module.exports = {
    page: async ctx => {
        return ctx.render('home', {title: 'My app title'});
    },
};
  1. 现在,当我要去这条路线时,我得到了错误。

预期行为 应该呈现主页

我的 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"
}

系统

我已经在最新版本的strapi上试过了。

标签: ejskoastrapi

解决方案



推荐阅读