Mongodb没有使用Nodejs

时间:2015-02-21 07:30:52

标签: node.js mongodb database

我试图通过浏览器上的html表单输入输入后打印人名和工作。该程序还允许通过同一页面上的简单链接删除和更新记录。但是,该程序无效。

我甚至试过创建一个" newapp"数据库(在服务器文件中创建)并用一些虚拟值填充,程序仍然无法正常工作!

以下是我的node.js文件

var express= require("express"),
http= require("http"),
bodyparser= require('body-parser'),
mongo= require("mongodb");

var app= express(),
db= new mongo.Db("newapp", 
                    new mongo.Server("localhost", 27017), 
                    {safe:true}, {auto_reconnect: true}),
people= db.collection("people");

app.use(bodyparser.urlencoded({extended: true}));

app.get("/", function(req, res){
    people.find().toArray(function(err, docs){
            if(err) 
                throw err;
            res.render("index.jade", {people: docs});   
    });
});

app.post("/", function(req, res){
    people.insert({name: req.body.name, job: req.body.job}, 
            function(err, doc){
                    if(err)
                        throw err;
                    res.redirect("/");
            });
});

app.listen(3000);

我的index.jade文件是:

form(method="POST")
    p Name:
        input(type="text", name="name")

    p Job:  
        input(type="text", name="job")

    p: button Add

if(typeof(people)!=="undefined")
    ul
            each person in people
                    li
                        h2= person.name+ " ("+ person.job+ ")"
                        p
                            a(href="/update/#{person._id}") Update
                            a(href="/delete/#{person._id}") Delete

else
    p No People

非常感谢任何帮助。非常感谢!

2 个答案:

答案 0 :(得分:0)

请检查这个希望这将有效..

 var express= require("express"),
    http= require("http"),
    bodyparser= require('body-parser'),
 var MongoClient = require('mongodb').MongoClient;
var connect = require('connect'),
    var app= express(),
   MongoClient.connect("mongodb://localhost:27017/newapp", function(err, db) {
  if(err) { return console.dir(err); }

              var people = db.collection('people');
    app.use(bodyparser.urlencoded({extended: true}));

    app.get("/", function(req, res){
        people.find().toArray(function(err, docs){
                if(err) 
                    throw err;
                res.render("index.jade", {people: docs});   
        });
    });

    app.post("/", function(req, res){
        people.insert({name: req.body.name, job: req.body.job}, 
                function(err, doc){
                        if(err)
                            throw err;
                        res.redirect("/");
                });
    });
  });  
    app.listen(3000);

答案 1 :(得分:0)

错误是我没有在程序的任何地方完成db.open(...)

正确的工作代码是:

var express= require("express"),
http= require("http"),
bodyparser= require("body-parser"),
mongo= require("mongodb");

var app= express(), 
db= new mongo.Db("newapp", new mongo.Server("localhost", "27017"), 
                             {safe:true}, {auto_reconnect: true});

app.use(bodyparser.urlencoded({extended: true}));

db.open(function(err, db){
    if(err)
        console.log(err);

    people= db.collection("people");

    app.get("/", function(req, res){
        var cursor= people.find();
        cursor.toArray(function(err, docs){
            if(err) 
                throw err;
            res.render("index.jade", {people: docs});   
        });
    });

    app.post("/", function(req, res){
        people.insert({name: req.body.name, job: req.body.job}, 
            function(err, doc){
                    if(err)
                        throw err;
                    res.redirect("/");
            });
    });

    app.get("/update/:id", function(req, res){
        people.findOne({_id: new mongo.ObjectID(req.params.id)}, 
            function(err, doc){
                    if(err) 
                        throw err;
                    res.render("update.jade", {person: doc});
            });
    });

    app.post("/update/:id", function(req, res){
        people.update({_id: new mongo.ObjectID(req.params.id)},{
                    name: req.body.name,
                    job: req.body.job
            }, function(err, item){
                    if(err)
                        throw err;
                    res.redirect("/");
            });
    });

    app.get("/delete/:id", function(req, res){
        people.remove({_id: new mongo.ObjectID(req.params.id)},
            function(err){
            if(err)
                throw err;
            res.redirect("/");
            });
    });
});

app.listen(3000, function(){
    console.log("Now Listening on port: 3000");
});

index.jade(在views文件夹中)是:

form(method="POST")
    p Name:
        input(type="text", name="name")

    p Job:  
        input(type="text", name="job")

    p: button Add

if(people!==NULL)
    ul
        each person in people
                li
                    h2= person.name+ " ("+ person.job+ ")"
                    p
                        a(href="/update/#{person._id}") Update&nbsp
                        a(href="/delete/#{person._id}") Delete

else
    p No People

update.js(在views文件夹中)是:

form(method="POST")
    p Name:
            input(type="text", name="name", value="#{person.name}")

    p Job:  
            input(type="text", name="job", value="#{person.job}")

    p: button Update