解決nodejs使用apidoc問題:Cannot read property 'Filter...' of undefined

apidoc是什么?

apidoc 是一個(gè)根據(jù)源代碼中api注解創(chuàng)建api文檔的工具,輕便易用。

問題背景

  • 現(xiàn)象:nodejs中使用apidoc在控制臺報(bào)如下錯(cuò)誤:
require.min.js:19 TypeError: Cannot read property 'Filter...' of undefined
    at Object.__ (locale.js?v=1579071667889:35)
    at Object.<anonymous> (handlebars_helper.js?v=1579071667889:43)
    at Object.eval [as main] (eval at createFunctionContext (handlebars.min.js?v=1579071667889:28), <anonymous>:6:97)
    at c (handlebars.min.js?v=1579071667889:27)
    at d (handlebars.min.js?v=1579071667889:27)
    at e (handlebars.min.js?v=1579071667889:28)
    at main.js:295
    at Object.execCb (require.min.js:30)
    at ea.check (require.min.js:18)
    at ea.<anonymous> (require.min.js:23)

頁面顯示為空白:


錯(cuò)誤截圖
  • 軟件環(huán)境:
    • apidoc 版本:0.19.1
    • 操作系統(tǒng) macOS 10.14.6
    • Google chrome 版本 79.0.3945.117(正式版本)
    • node v10.16.3
  • 測試源代碼
const Koa = require('koa');
const util = require('util');
const app = new Koa();
const helper = require('./helper');

let logger = helper.getLogger('logTest');

const router = require('koa-router')();

const handler = async (ctx, next) => {
    try {
        console.info("==>handler");
        await next();
        console.info("<==handler");
    } catch (e) {
        console.info('e.err:', e.err);
        console.info('e.status:', e.status);
        ctx.response.statusCode = e.status || e.err || 500;
        ctx.response.body = 'errrrrrrrrr';
        // console.error("error::", e);
        // console.error("error::", e.toString());
        // console.error("error::", e.toLocaleString());
        // console.error("error::", e.status);
        // console.error("error::", typeof e);
        // console.error("error::", e.valueOf());
        // console.error('err msg:', e.message);
        console.info("1<==handler");
        // ctx.app.emit('error', e, ctx)
        console.log(e.message === 'file_not_found');
        ctx.app.emit(e.message, new Error("log file can't be found"), ctx);
    }
};

app.use(handler);


/**
 * @api {post} /api/User/register 用戶注冊
 * @apiDescription 用戶注冊
 * @apiName Register
 * @apiGroup User
 * @apiParam {string} name 用戶名
 * @apiParam {string} password 密碼
 * @apiVersion 1.0.0
 */
app.use(router.get('/test', async ctx => {
    console.log("test log");
    ctx.throw(new Error(util.format('file_not_found:%s', 'asdfasdf')));
}).routes());

app.listen(3000);
console.log("ready for service");

app.on('file_not_found', (err, ctx) => {
    // console.error("file not found error:", err, ctx);
    // logger.error("file not found error: %s, ctx:%s", err, ctx);
    logger.error("err:", err);
    logger.info("--============================");
    logger.error("%s, ctx:%j", err.stack, ctx);
    // console.error('err stack:', err.stack);
});
  • 使用到的package.json
{
  "name": "koa-demos",
  "version": "1.0.0",
  "description": "a collection of simple demos of Koa",
  "scripts": {
    "dev": "cross-env NODE_ENV=dev nodemon demos/test/logtest.js",
    "prod": "cross-env NODE_ENV=prod node demos/test/logtest.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "apidoc": {
    "template": {
        "forceLanguage": "zh-cn"
    },
    "title": "apiDoc在瀏覽器中的標(biāo)題",
    "url": "api.github.com/v1"
  },
  "keywords": [
    "Koa",
    "Node"
  ],
  "author": "Li Yi",
  "dependencies": {
    "cross-env": "^6.0.3",
    "express-validator": "^6.3.0",
    "fs.promised": "^3.0.0",
    "koa": "^2.3.0",
    "koa-body": "^2.3.0",
    "koa-bodyparser": "^4.2.1",
    "koa-compose": "^4.0.0",
    "koa-parameter": "^3.0.1",
    "koa-route": "^3.2.0",
    "koa-router": "^7.4.0",
    "koa-schema": "0.0.1",
    "koa-static": "^4.0.1",
    "log4js": "^6.1.0"
  },
  "devDependencies": {
    "apidoc": "^0.19.1",
    "jsdoc": "^3.6.3"
  }
}
  • 運(yùn)行的命令
// -i demos/test是測試文件所在的目錄
// -f ".*\\.js$" 是用來指定要生成api的文件格式的
// -o ./apidoc是用來指定生成文件的目錄的
 apidoc -i demos/test -f ".*\\.js$" -o ./apidoc

原因

在0.19.1版本中沒有zh-cn這個(gè)編碼了,得設(shè)置成為zh_cn。
之所以會設(shè)置成為這個(gè)錯(cuò)誤的編碼是因?yàn)樵?a target="_blank">這篇博客中看到的教程設(shè)置的,現(xiàn)在版本迭代之后,zh-cn已經(jīng)不能用了。
參見官方現(xiàn)有的locales

經(jīng)驗(yàn)教訓(xùn)

  • 能看官網(wǎng)教程的時(shí)候還是盡量看官網(wǎng)的,畢竟網(wǎng)上散落的教程有可能過時(shí)了,但是官網(wǎng)的總會是最新的。。
  • 大家在博客中寫教程的時(shí)候,盡量帶上軟件的版本號信息,環(huán)境信息,不然容易誤導(dǎo)人
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 前端和后端注釋文檔生成 前端和后端的 函數(shù)及api 說明文檔生成總結(jié),持續(xù)更新中 by Qzx 參考網(wǎng)址 jsD...
    流云012閱讀 12,163評論 0 1
  • 寒徹殘冬霧泛渾,遠(yuǎn)街近道罕行人。 暗云有意難留雪,深院無風(fēng)不掃塵。 過客經(jīng)心嚴(yán)蓋臉,家親依暖緊關(guān)門。 老翁所樂知何...
    sx老魚閱讀 186評論 0 0
  • 當(dāng)一個(gè)人獲得另一個(gè)人的贊美時(shí),他便感覺獲得了社會支持,從而增強(qiáng)了自我價(jià)值,變得自信、自尊,獲得一種積極向上的動(dòng)力,...
    青山憶舊閱讀 165評論 0 1
  • 那一年我即將升入初三。因?yàn)榛A(chǔ)不佳,初二增加了一門物理,老師的節(jié)奏又快,漸漸地我越來越吃不消,干脆偃旗息鼓...
    花房姑娘1987閱讀 343評論 0 0
  • 在我小兒子很小的時(shí)候,有一次我因?yàn)楦壬娫捰行┎挥淇?,心情莫名的不好,沒有買菜也沒有煮飯,天都黑了還躺在床上不...
    玉米嬸閱讀 11,470評論 133 185

友情鏈接更多精彩內(nèi)容