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

问题描述

我遇到了一个问题。

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

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

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

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

{"record":{"_id":"60e05b4dd15ebd54547b10b4","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.',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.',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)

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