Trying a basic thing in Koa2, fetching results from MongoDB and sending it in response. Following is my code with koa-router.
If I try to send the record in ctx.body, the result is always "Not Found" response. Help!!
import Router from 'koa-router';
import Users from '../models/users';
const router = new Router();
router.get('/users', async (ctx, next) => {
const user = await Users.find();
ctx.body = 'the result'; // how do I populate the user record here
//ctx.body = JSON.stringify(user) always returns in a "Not Found" response
});
export default router;
For reference my model users.js
import mongoose from 'mongoose';
const User = new mongoose.Schema({
name: {
type: String
}
});
export default mongoose.model('users', User)