怎么不能吧填写的信息传到mongodb中,填写的信息能在控制台显示出来
发布于 9 年前 作者 martin823823 3795 次浏览 最后一次编辑是 8 年前 来自 问答

/**

  • Module dependencies. */ var model = require(‘model’); var express = require (‘express’), routes = require (’./routes’), http = require (‘http’), path = require (‘path’), mongoose = require (‘mongoose’);

var bodyParser = require(‘body-parser’)

var app = express (); var db = mongoose.createConnection (‘mongodb://localhost/test’);

var Schema = mongoose.Schema; var ObjectId = Schema.ObjectId;

var Task = new Schema ({ task: {type:String} },{ collection:‘test’ });

var Task = db.model (‘test’, Task );

// all environments

app.set (‘view engine’, ‘jade’);

// parse application/x-www-form-urlencoded app.use (bodyParser.urlencoded ({ extended: false }))

// parse application/json app.use (bodyParser.json ())

app.get (’/tasks’, function (req, res ){ Task.find ({}, function (err, docs ) { res.render (‘tasks/index’, { title: ‘Todos index view’, docs: docs }); }); });

app.get (’/tasks/new’, function (req, res ){ res.render (‘tasks/new.jade’, { title: ‘New Task’ }); });

app.post (’/tasks’,function (req, res ){ var task = new Task(req.body ); module.exports = task; task.save (function (err ) { if (!err ) { res.redirect (’/tasks’); } else { res.redirect (’/tasks/new’); } }); console.log (req.body ); });

app.listen (3000,function (err ){ if (err ){ console.log (err ); return err; } }); console.log (‘the project run 3000’);

1 回复

代码格式化下吧 ,这样谁愿意看。。。

回到顶部