I am trying to manipulate a document that returns a query to mongoDB through mongoose but I manage to get it. I am developing with Node.js with Koa framework and Mongoose as ODM.
The code is :
const Router = require('koa-router');
const RestaurantModel = require('models/restaurant.model');
class RestaurantRouter{
static async get(ctx,next){
ctx.body = await RestaurantModel.find();
}
static async getById(ctx){
ctx.body='get';
}
}
const router = new Router({prefix:'/restaurant'});
router.get('/', RestaurantRouter.get);
router.get('/:id', RestaurantRouter.getById);
module.exports = router;
I would need to be able to manipulate the document that RestaurantModel.find(); and embed it inside another larger document and send it to the response body, something like
ctx.body = await {feature : RestaurantModel.find()};
I can't find a way since RestaurantModel.find() doesn't return the document itself but a Query object from which I can't get the document.
Any suggestion?
Thank you!
In Koa, the Content-Type is implicit according to the value you assign to
ctx.body
. In your case, you must assign an object to it so that it returns a JSON. I don't see any problem in your case, except for architectural details. You just need to wrap what the model returns in another object:Which will produce a response like.