typescript Fastify:AvvioError [错误]:插件未及时启动

5ssjco0h  于 2023-03-31  发布在  TypeScript
关注(0)|答案(1)|浏览(119)

我目前正在处理一个typescript/fastify项目(这是一个糟糕的堆栈选择imo).我得到这个错误,而注册路由:

/home/20015007/Documents/Ecole/EcoWeb/econos-back/node_modules/avvio/plugin.js:122
      const err = new AVV_ERR_READY_TIMEOUT(name)
                  ^
AvvioError [Error]: Plugin did not start in time: 'declareHealthRoutes'. You may have forgotten to call 'done' function or to resolve a Promise
    at Timeout._onTimeout (/home/20015007/Documents/Ecole/EcoWeb/econos-back/node_modules/avvio/plugin.js:122:19)
    at listOnTimeout (node:internal/timers:559:17)
    at processTimers (node:internal/timers:502:7) {
  code: 'AVV_ERR_READY_TIMEOUT',
  fn: [Function: declareHealthRoutes]
}

下面是我的代码:
routes.ts:

import { declareArticleRoutes } from "./articlesRoutes";
import { declareContactRoutes } from "./contactRoutes";
import { declareAdminRoutes } from "./adminRoutes";
import { verifyJWT} from "../services/tokenService";
import { declareHealthRoutes } from "./healthRoutes";
function declareRoutes(fastify: FastifyInstance) {
    fastify.register(declareArticleRoutes);
    fastify.register(declareContactRoutes);
    fastify.register(declareHealthRoutes);
    fastify.register(declareAdminRoutes);
}

declareHealthRoute:

import {FastifyInstance, FastifyReply, FastifyRequest} from "fastify";

const healthSchema = {
    response: {
        200: {}
    }
};

function declareHealthRoutes(fastify: FastifyInstance): FastifyInstance {

    fastify.route({
        method: 'GET',
        url: '/health2',
        schema: healthSchema,
        handler: (req: FastifyRequest, reply: FastifyReply) => {
            return reply.status(200).send({});
        },
    });
    return fastify
}

export { declareHealthRoutes };

我所有的路由都是用这种简单的方式声明的。
只是想说:我知道如何让这段代码工作(declareHealthRoutes(fastify)确实注册了我的路由),但我想知道为什么这不起作用,如果我没有使用错误的注册方式。
任何帮助都是感激的!

jum4pzuy

jum4pzuy1#

function declareHealthRoutes必须是异步的或者调用回调:

async function declareHealthRoutes(fastify) { }

// or
function declareHealthRoutes(fastify, opts, next) {
  next()
}

否则,fastify不知道插件在做什么。

相关问题