当对象从 findOne 发回时,Fastify 响应不是打印机

Fastify response is not printer when object is send back from findOne

我遇到了问题。

我从 Fastify findOne 返回一个这样的对象(我检查过它是 JS 中的一个 instanceOf 对象)。

{"_id":"60e05b4dd15ebd54547b10b4","server":"10.10.10.10","port":3000,"user":"admin","role":"admin"}

现在,当我在另一个对象的记录字段中按原样传递对象时,尝试使用某些 API 测试器时不会打印它(我正在尝试使用 Talend API 测试器 Chrome).

只有当我把它放在 JSON.stringfy(whole_object) 下时,它才会被记录为:

{"record":{"_id":"60e05b4dd15ebd54547b10b4","server":"10.10.10.10","port":3000,"user":"admin","role":"admin"},"tenancyResourceId":"resource-account-0Sbc3gZI"}

我的响应模式:

response: {
                200: {
                    $ref: 'metaservices-record#',
                    description: 'Meta Service Record.'
                },
                '4xx': {
                    $ref: 'error-response#',
                    description: 'Error response'
                }
            }

错误响应参考如下:

 fastify.addSchema({
        $id: 'metaservices-record',
        title: "MetaServiceRecord",
        type: 'object',
        description: 'Individual Meta Service record.',
        properties: {
            'record': {
                description: '(Opeque) Meta Service record.',
                type: 'object',
                example: '{ key1: value1, key2: value2, ....., key-n: value-n}',
            },
            'tenancyResourceId': {
                description: 'Meta Service tenancy resource specifier. This is inserted by MetaService infra and for MetaService infrastructure usage only.',
                type: 'string',
                example: 'resource-x67ety733iu',
            }
        },
    });

返回对象的我的服务代码片段(在 fastify.route .. GET 中)

handler: async (request, reply) => {
            const result = await dbDriver.find(reply, getDbName(request.params.serviceName), 
                request.params.collectionName, request.parsedQueryUrl, request.params.recordId, false);
            const returnResult = {'record': result, 'tenancyResourceId': `resource-${request.params.accountId}`};
            reply.status(200).send(JSON.stringify(returnResult));  // <----
        }

看到最后一行,我不得不做一个JSON.stringify。

如果我省略 stringfy,它不会打印任何内容,如下所示 API 输出:

{
"record":{},  // <- Empty
"tenancyResourceId": "resource-account-0Sbc3gZI"
}

谢谢, 普拉迪普

问题出在您的 metaservices-record 架构中。您必须添加 additionalProperties 参数才能查看未映射到您的 JSON 架构中的所有字段。


const fastify = require('fastify')({ logger: true })

fastify.get('/', {
  schema: {
    response: {
      200: {
        $ref: 'metaservices-record#',
        description: 'Meta Service Record.'
      }
    }
  }
}, async (request, reply) => {
  return { record: { _id: '60e05b4dd15ebd54547b10b4', server: '10.10.10.10', port: 3000, user: 'admin', role: 'admin' }, tenancyResourceId: 'resource-account-0Sbc3gZI' }
})

fastify.addSchema({
  $id: 'metaservices-record',
  title: 'MetaServiceRecord',
  type: 'object',
  description: 'Individual Meta Service record.',
  properties: {
    record: {
      description: '(Opeque) Meta Service record.',
      type: 'object',
      additionalProperties: true,
      example: '{ key1: value1, key2: value2, ....., key-n: value-n}'
    },
    tenancyResourceId: {
      description: 'Meta Service tenancy resource specifier. This is inserted by MetaService infra and for MetaService infrastructure usage only.',
      type: 'string',
      example: 'resource-x67ety733iu'
    }
  }
})

fastify.listen(5050)

此外,我建议 return 您想要发回客户端的对象,而不是在 async 处理程序中调用 reply.send 作为 documented