A
A
Anton Ulanov2016-03-13 23:41:21
Node.js
Anton Ulanov, 2016-03-13 23:41:21

How to write route in Hapi+hapi-swagger?

Good afternoon, I decided to learn hapi.js and ran into a situation that was incomprehensible to me.
route looks like:

server.route({
  method: 'GET',
  path: 	'/api/user',
  config: {
    tags: ['api'],
    description: 'Get All User data',
    notes: 'Get All User data'
  },
  handler: function (request, reply) {
    UserModel.find({}, function (error, data) {
      if (error) {
        reply({
          statusCode: 503,
          message: 'Failed to get data',
          data: error
        });
      } else {
        reply({
          statusCode: 200,
          message: 'User Data Successfully Fetched',
          data: data
        });
      }
    });
  }
});

and when requested it displays:
{
  "statusCode": 200,
  "message": "User Data Successfully Fetched",
  "data": [
    {
      "_id": "56e5bfa46811f3783b43993a",
      "uuid": "sldkgskldfg",
      "phone": 48792452359,
      "__v": 0
    }
  ]
}

lines bother me
"statusCode": 200,
  "message": "User Data Successfully Fetched",
  "data": [

after all, with the correct json output, they should not be. How to fix it? thanks in advance for the tip

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
pomeo, 2016-03-14
@pomeo

Why shouldn't they be, you bring them out yourself

reply({
  statusCode: 200,
  message: 'User Data Successfully Fetched',
  data: data
});

And at the output you get a completely valid json
{
  "statusCode": 200,
  "message": "User Data Successfully Fetched",
  "data": [
    {
      "_id": "56e5bfa46811f3783b43993a",
      "uuid": "sldkgskldfg",
      "phone": 48792452359,
      "__v": 0
    }
  ]
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question