我正在使用MEAN堆栈进行应用程序。我的问题是我无法使用mongoose访问我的数据库,我不知道为什么。我复制了以前的应用程序的代码(完美地工作)并将其改编为我的应用程序。它连接到数据库。我比较了一百次代码,似乎没有任何错误。
我们去找代码
服务器:
.delete
路由器和api代码:
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
require('./models/EnergyHive.js');
mongoose.connect('mongodb://10.70.64.56/PowerConsumption'); //connection a la Database
mongoose.connection.on("open", function(){
console.log("=---= mongodb is connected!"); //confirmation de connection
});
var routes = require('./routes/index');
var api = require('./routes/api');
var users = require('./routes/users');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
// uncomment after placing your favicon in /public/images
//app.use(favicon(path.join(__dirname, 'public', '/images/favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use('/statics', express.static(path.join(__dirname, 'public')));
app.use('/', routes);
app.use('/api', api);
app.use('/users', users);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
module.exports = app;
// API
var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var EnergyHive = mongoose.model('EnergyHive');
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
console.log("=---= Bonjour");
});
module.exports = router;
角度
var express = require('express');
var api = express.Router();
var mongoose = require('mongoose');
var EnergyHive = mongoose.model('EnergyHive');
console.log("=---= API start");
api.get('/power_monitor', function(req, res, next){
console.log("=---= power_monitor");
var day = new Date().getDate();
var month = new Date().getMonth()+1;
var year = new Date().getFullYear();
console.log("=---= day : ", day);
console.log("=---= month : ", month);
console.log("=---= year : ", year);
EnergyHive.find({"consult_day": day, "consult_month": month, "consult_year": year}, function(err, Data){
console.log("=---= in the collection !");
if(err){
console.log("=---= Error" + err);
return next(err);
}
else {
console.log("=---= docs recuperé !", Data);
}
res.json(Data);
});
});
module.exports = api;
我知道很多代码,抱歉。 谢谢你的帮助:)