[英]NodeJS + Express + Mongoose(MongoDB) Database Insert Error
[英]nodejs express mongoose connect to db error
我使用nodejs express和mongoose连接mongodb,我在根文件夹中创建了一个config.js。我试图在db.js中导出db connect,并尝试在adminController.js中导入
但是,当我运行服务器并刷新浏览器时,它会向我记录一些错误,而不是在终端上记录我的日志。
config.js
module.exports = {
cookieScret:'ThreeKingdoms',
db:'threekingdoms',
host:'localhost',
port:27017
}
db.js
var mongoose = require('mongoose'),
config = require('../config'),
connection = mongoose.connection;
module.exports = function(mongoose){
return mongoose.connect('mongodb://'+config.host+'/'+config.db);
}
adminController.js
var express = require('express'),
router = express.Router(),
mongoose = require('mongoose'),
mainInfo = require('../models/admin'),
db = require('../models/db');
router.get('/', function(req, res) {
res.render('admin', { title: 'hey im here!how are you' });
db.on('error',console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log(db)
});
console.log(db)
});
router.post('/',function(req,res,next){
//console.log(req.body)
var mainInfo = mongoose.model('mainInfo');
})
module.exports = router;
我是nodejs的新手,所以请帮助我,谢谢
首先,您似乎没有安装kerberos。 请运行npm install kerberos。 还行
Can't set headers after they're sent
建议您在将响应发送回客户端后尝试修改/发送该响应,这就是您遇到的错误。
终于现在可以使用了,但是与“ kerberos”无关紧要,这是我使用错误的方式来导出模块。终端仍然记录我“ kerberos undefined”但网络可以工作,我想也许我会尝试一段时间弄清楚这个问题。感谢您的关注
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.