V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
martin823823
V2EX  ›  Node.js

为什么我不能插入数据到 mongodb 中???

  •  
  •   martin823823 · 2015-08-28 08:51:28 +08:00 · 3188 次点击
    这是一个创建于 3375 天前的主题,其中的信息可能已经有所发展或是发生改变。
    /**
    * Module dependencies.
    */

    var express = require ('express'),
    routes = require ('./routes'),
    http = require ('http'),
    path = require ('path'),
    mongoose = require ('mongoose');

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

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

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

    var Task = db.model ('test', Task );

    // all environments

    app.set ('view engine', 'jade');




    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 );
    console.log (req.body );
    task.save (function (err ) {
    if (!err ) {
    res.redirect ('/tasks');
    }
    else {
    res.redirect ('/tasks/new');
    }
    });
    });

    app.listen (3000,function (err ){
    if (err ){
    console.log (err );
    return err;
    }
    });
    console.log ('the project run 3000');
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2855 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 08:46 · PVG 16:46 · LAX 00:46 · JFK 03:46
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.