使用Moogoose连接Mongodb
1.安装:
npm install mongoose --save-dev
2.koa项目中创建models文件夹:
创建Users.js
let mongoose = require('mongoose');
let Users = mongoose.model('users', new mongoose.Schema({
email: String,
pwd: String
},{_id:true}));
module.exports = Users;
3.routers/users.js中写:
let mongoose = require('mongoose');
let Users = require('../models/Users')
router.post('/register', async function (ctx, next) {
// let email = ctx.request.body['email'];
// let pwd = ctx.request.body['pwd'];
// ctx.body = 'email:'+email+",pwd:"+pwd;
let conn = mongoose.connect('mongodb://localhost/partner');
delete ctx.request.body['repwd'];
let users = new Users(ctx.request.body);
let rs = await users.save();
console.log(rs);
ctx.body = rs;
})