martin823823
V2EX  ›  Node.js

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

  •  
  •   martin823823 · Aug 28, 2015 · 3923 views
    This topic created in 3936 days ago, the information mentioned may be changed or developed.
    /**
    * 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');
    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2904 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 09:12 · PVG 17:12 · LAX 02:12 · JFK 05:12
    ♥ Do have faith in what you're doing.