2

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)
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Zaje
  • 2,281
  • 5
  • 27
  • 39
  • Found the fix, my bad. Working on my isomorphic app Koa wasnt reaching this route at all when await called on db. Moved the isopmorphic route to the bottom and it works :) Admin can delete the question. – Zaje Aug 09 '16 at 01:34

0 Answers0