首页 > 解决方案 > 不支持 Fastify 所需的路由参数

问题描述

我有一条这样的路线:

    // collection GET
    fastify.route({
        method: 'GET',
        url: `${constants.EXTERNAL_PATH}/:serviceName/:collectionName/account/:accountId`,
        schema: {
            description: 'Get all documents from the specified table for the specified service database and account.',
            summary: 'Get all documents from the specified table.',
            tags: ['persistence'],
            headers: {
                $ref: 'authorization-user#'
            },
            params:{
                type: 'object',
                properties: {
                    serviceName:    { type: 'string' },
                    collectionName: { type: 'string' },
                    accountId:      { type: 'string' }
                },
                required: ['serviceName', 'collectionName', 'accountId'],
            },
            response: {
                200: {
                    properties: {
                        'documents': {
                            description: 'All the retrieved documents from the specified table for the specified service database and account.',
                            type: 'array',
                            items: {
                                $ref: 'persistence-response-doc#',                  
                            }
                        },
                    },
                },
                '4xx': {
                    $ref: 'error-response#',
                    description: 'Error response'
                }
            }
        },
        handler: async (request, reply) => {
           // Logic goes here //
        }
    });

现在我的期望是:

这应该有效:

http://localhost:9011/persistence/external/myservice/mycollection/account/my_actn_id
As it has the service as myservice, collection as mycollection and accountId as my_actn_id

但是,这应该会因路由错误而失败(例如,当我缺少必需的 :accountId):

http://localhost:9011/persistence/external/myservice/mycollection/account/  <== should fail

然而,两者都通过了,返回相同的收集结果。

我在这里想念什么?

谢谢, 普拉迪普

标签: fastify

解决方案


推荐阅读