Koa2 + Mongoose + async / await

时间:2016-08-08 22:21:28

标签: mongoose async-await ecmascript-next koa2

在Koa2中尝试基本的东西,从MongoDB中获取结果并将其作为响应发送。以下是我的koa-router代码。

如果我尝试在ctx.body中发送记录,结果总是“未找到”响应。帮助!

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;

供我参考我的模型users.js

import mongoose from 'mongoose';

const User = new mongoose.Schema({
  name: {
    type: String
  }
});

export default mongoose.model('users', User)

0 个答案:

没有答案