有什么问题?我无法使用mongoose查询或写入数据库。例如,如果我尝试保存或添加新用户,该功能永远不会完全完成,它只是挂在那里......没有报告错误(据我所知)。示例如下所示。
到目前为止我做了什么?
mongoose.connect
成功连接到mLab数据库。 mongoose.connection.readyState
是什么(即0或1),输出似乎都是相同的,而不会抛出错误。 其他更新:
这可能是我使用的版本的问题吗?
我不确定要显示什么代码,但要保持简短,我已经包含了主要功能:
app.js
var conn = process.env.MONGOLAB_URI;
var mongoConnection = mongoose.connect(conn, {useMongoClient: true}, function (err, db) {
if (err) {
console.log('Unable to connect to MongoDB: ', err);
} else {
console.log('Connection has been established...');
}
});
user_controller.js
module.exports.signup = function (req,res) {
var newuser = new User;
newuser.set('username', (req.body.username).toLowerCase() );
newuser.set('hashed_password', hashPW(req.body.password));
newuser.set('email', (req.body.email).toLowerCase());
newuser.set('admin',false);
debug("User information entered: " + newuser);
newuser.save(function (err,newuser) {
if (err) {
try {
if (err.code==11000) res.render('signup', { message: 'Sorry, someone is using that username or email already.'} );
} catch(e) {
debug(e);
}
} else {
req.session.user = newuser.id;
req.session.username = newuser.username;
req.session.msg = 'Authenticated as ' + newuser.username;
debug("User information saved. Redirecting...");
res.redirect('/');
}
});
};
这是输出的样子
Connection has been established...
app:server User information entered: { admin: false,
app:server email: 'blahblah@gmail.com',
app:server username: 'blaaaahhh',
app:server _id: 5957fddbc854c056c5b695f4 } +0ms
POST /signup - - ms - -
app:server User information entered: { admin: false,
app:server email: 'blahblah@gmail.com',
app:server username: 'blaaaahhh',
app:server _id: 5957fe53c854c056c5b695f5 } +2m
POST /signup - - ms - -