Output [object Object] when trying to identify 'body' parameter in path's apiDoc
See original GitHub issueI was trying to do a PUT /inventories/{id} endpoint that will accept request body as:
{ "inventory": 100 }
I defined the apiDoc in each path js file, and this is the one for PUT.
// paths/inventories/{id}.js
const inventoryByIdPath = (inventoriesService) => {
const PUT = (req, res) => {
const id = _.get(req, 'params.id');
const data = _.get(req, 'params.data');
...
};
PUT.apiDoc = {
...
parameters: [
{
in: 'path',
name: 'id',
description: 'productId',
required: true,
type: 'string'
},
{
in: 'body',
name: 'data',
description: 'Inventory Data',
required: true,
schema: {
$ref: '../../configs/schemas.json#/Inventory'
}
}
]
...
}
// schemas.json:
{
"Inventory": {
"properties": {
"inventory": {
"type": "number"
}
}
}
}
However, when I call this endpoint, it will always return html error response instead of json:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>[object Object]</pre>
</body>
</html>
and console would have logged: [object Object]
after I send the request, which I didn’t log it anywhere in code.
After setting the break point, I found out it didn’t even make it to the line: const PUT = (req, res) => {...}
Though after removing:
{
in: 'body',
name: 'data',
description: 'Inventory Data',
required: true,
schema: {
$ref: '../../configs/schemas.json#/Inventory'
}
}
It would work normal, but it won’t have ‘body’ in req.
I tried to find more similar examples online, but only found those provided in express-openapi docs. Having no clue where would it go wrong. Any ideas? And is there anyway I could know what’s inside that mystery [object, Object]?
Using these packages:
{
"express": "^4.16.2",
"express-openapi": "^1.2.0"
}
Issue Analytics
- State:
- Created 6 years ago
- Comments:10 (5 by maintainers)
Top GitHub Comments
@BkunS @jsdevel I think i traced down the issue.
TLDR;: you might have no error handling middleware defined
I was able to reproduce this with the following 4 lines of code:
Launch http://localhost:9095 and see
[object Object]
.The same happens with express-openapi when a validation fails. The validator internally triggers
next({ status : 400, ...someValidationErrorInfo })
in case of a failed validation. When you have no express error handler(err, req, res, next) => {}
or have not defined an express-openapi errorMiddleware express tries to convert the error in your case something like
{ status: 400, message: 'validation failed' }
to a string and send it as error response.@alrik @jsdevel Thanks! With the error handling middleware, I was able to know what’s inside the error now, and realized that this whole thing is just because I also didn’t use body-parser which I once thought was built in with express. This can be closed.