ReferenceError:客户端未定义Node.js Mongodb

时间:2013-11-19 19:26:00

标签: javascript node.js mongodb

我正在创建一个客户端管理系统,我正在使用RESTful方法来创建仪表板或排序。 但是,不幸的是,当我尝试启动看起来像这样的节点服务器时,我在终端中收到错误

   /Users/taylormoore/Desktop/Fitness-app/server.js:11
app.get('/clients', client.findAll);
                    ^
ReferenceError: client is not defined
    at Object.<anonymous> (/Users/taylormoore/Desktop/Fitness-app/server.js:11:21)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)

server.js看起来像

var express = require('express');
    clients = require('./routes/clients')

var app = express();

app.configure(function () {
    app.use(express.logger('dev'));     /* 'default', 'short', 'tiny', 'dev' */
    app.use(express.bodyParser());
});

app.get('/clients', client.findAll);
app.get('/clients/:id', client.findById);
app.post('/clients', client.addclient);
app.put('/clients/:id', client.updateclient);
app.delete('/clients/:id', client.deleteclient);

app.listen(3000);
console.log('Listening on port 3000...');

Client.js看起来像

 var mongo = require('mongodb');

var Server = mongo.Server,
    Db = mongo.Db,
    BSON = mongo.BSONPure;

var server = new Server('localhost', 27017, {auto_reconnect: true});
db = new Db('clientdb', server);

db.open(function(err, db) {
    if(!err) {
        console.log("Connected to 'clientdb' database");
        db.collection('clients', {strict:true}, function(err, collection) {
            if (err) {
                console.log("The 'clients' collection doesn't exist. Creating it with sample data...");
                populateDB();
            }
        });
    }
});

exports.findById = function(req, res) {
    var id = req.params.id;
    console.log('Retrieving client: ' + id);
    db.collection('clients', function(err, collection) {
        collection.findOne({'_id':new BSON.ObjectID(id)}, function(err, item) {
            res.send(item);
        });
    });
};

exports.findAll = function(req, res) {
    db.collection('clients', function(err, collection) {
        collection.find().toArray(function(err, items) {
            res.send(items);
        });
    });
};

exports.addclient = function(req, res) {
    var client = req.body;
    console.log('Adding client: ' + JSON.stringify(client));
    db.collection('clients', function(err, collection) {
        collection.insert(client, {safe:true}, function(err, result) {
            if (err) {
                res.send({'error':'An error has occurred'});
            } else {
                console.log('Success: ' + JSON.stringify(result[0]));
                res.send(result[0]);
            }
        });
    });
}

exports.updateclient = function(req, res) {
    var id = req.params.id;
    var client = req.body;
    console.log('Updating client: ' + id);
    console.log(JSON.stringify(client));
    db.collection('clients', function(err, collection) {
        collection.update({'_id':new BSON.ObjectID(id)}, client, {safe:true}, function(err, result) {
            if (err) {
                console.log('Error updating client: ' + err);
                res.send({'error':'An error has occurred'});
            } else {
                console.log('' + result + ' document(s) updated');
                res.send(client);
            }
        });
    });
}

exports.deleteclient = function(req, res) {
    var id = req.params.id;
    console.log('Deleting client: ' + id);
    db.collection('clients', function(err, collection) {
        collection.remove({'_id':new BSON.ObjectID(id)}, {safe:true}, function(err, result) {
            if (err) {
                res.send({'error':'An error has occurred - ' + err});
            } else {
                console.log('' + result + ' document(s) deleted');
                res.send(req.body);
            }
        });
    });
}

/*--------------------------------------------------------------------------------------------------------------------*/
// Populate database with sample data -- Only used once: the first time the application is started.
// You'd typically not find this code in a real-life app, since the database would already exist.
var populateDB = function() {

    var clients = [
    {
        name: "Bill",
        age: "23",
        email: "Bill@gmail.com",
        phone: "233-229-7805",
        description: "The aromas of fruit and spice...",
        picture: "saint_cosme.jpg"
    },
    {
        name: "Bill",
        age: "23",
        email: "Bill@gmail.com",
        phone: "233-229-7805",
        description: "The aromas of fruit and spice...",
        picture: "lan_rioja.jpg"
    }];

    db.collection('clients', function(err, collection) {
        collection.insert(clients, {safe:true}, function(err, result) {});
    });

};

我刚开始使用Stack,并且我并不十分熟悉如何使用它。请帮助!

0 个答案:

没有答案