0
你好,我知道這個問題在stackoverflow中很常見,但我通過所有人,我真的不能得到什麼是我的錯碼。Node.js錯誤:Route.get()需要回調函數,但有一個[對象未定義]
這是我的主要server.js
var dbUri = process.env.MONGODB_URI;
var app = express();
var PORT = process.env.PORT || 3001;
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
if (app.settings.env === 'development') {
dbUri = 'mongodb://localhost/barsDb';
}
mongoose.connect(dbUri, function (err, res) {
if (err) {
console.log('Erorr connection to database ' + dbUri + '.' + err);
} else {
console.log('Connected to database on ' + dbUri + "\n");
}
});
//app.use(require('./routes.bars'));
require('./routes.bars')(app); //error here reciving undefined
// connect to server
app.listen(PORT, function() {
console.log('Listening to port ' + PORT + '...');
});
這些都是我routes.js
var bars = require('./controllers/controller.bar');
var mongoose = require('mongoose');
module.exports = function(app) {
\t app.route('/').get(bars.getMain); //error in this line it is returning undefinded.
\t app.route('/bars').get(bars.getBars);
\t app.route('/bars').post(bars.addBar);
\t app.route('/bars/:id').put(bars.updateBarById);
\t app.route('/bars/:loc').get(bars.getByLocation);
\t app.route('/bars/:id').get(bars.getBarById);
\t app.route('/bars/:id').delete(bars.deletBarById);
};
這是我的控制器:
var mongoose = require('mongoose');
var dbModel = require('./../model/bars.db');
var path = require('path');
var _ = require('underscore');
// main page
module.getMain = function (req, res) {
res.sendFile(path.join(__dirname + "/../public/index.html"));
};
// post new bar
module.addBar = function (req, res) {
var body = _.pick(req.body,'name', 'address', 'phone', 'barType', 'ambient', 'options', 'loc');
console.log(body);
var newBar = new dbModel(body);
newBar.save(function (err) {
if (err) throw err;
//res.send('Bar Created');
res.status(201).send();
});
};
// get all bars
module.getBars = function (req, res) {
dbModel.find({},function (err, bars) {
if (err) throw err;
res.json(bars);
//res.status(200).send();
});
};
//get bars by location
module.getByLocation = function (req, res) {
var barLoc = req.params.loc.split(",");
var barLocLon = parseFloat(barLoc[0]);//.toFixed(5);
var barLocLat = parseFloat(barLoc[1]);//.toFixed(5);
barLoc = []; barLoc.push(barLocLon); barLoc.push(barLocLat);
dbModel.find({
loc: {$gt:[barLocLon - 0.0200, barLocLat - 0.0200], $lt:[barLocLon + 0.0200, barLocLat + 0.0200]}
}, function (err, bars) {
if (err) throw err;
res.json(bars);
res.status(200).send();
});
};
// get bar by id:
module.getBarbyId = function (req, res) {
var barId = req.params.id;
dbModel.findById(barId, function (err, bar) {
if (err) throw err;
res.json(bar);
res.status(200).send();
});
};
// update bar by id:
module.updateBarById = function (req, res) {
var barId = req.params.id;
var body = _.pick(req.body,'name', 'address', 'phone', 'barType', 'ambient', 'options', 'loc');
dbModel.findById(barId, function (err, bar) {
if (bar) {
bar.save(function (err) {
if (err) throw err;
});
}
});
dbModel.findByIdAndUpdate(barId, {$set:req.body}, function (err, bar) {
if (err) throw err;
res.send('Updated');
});
};
// delete bar by id:
module.deleteBarById = function (req, res) {
var barId = req.params.id;
//console.log(barId);
dbModel.findByIdAndRemove(barId, function (err) {
if (err) throw err;
res.send('Deleted id ' + barId);
});
};
,你必須使用你的控制器出口:exports.myFunction =函數(){...}或使用模塊。出口= {myFunction:功能(){...},...} –
哦,我的上帝......我是個白癡。謝謝何塞。 –
根本不是白癡!這件事情發生!不客氣! =) –