我有一个简单的用户架构:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var userSchema = new Schema({
username: { type: String, required: true, unique: true },
email: { type: String, required: true, unique: true },
password: { type: String, required: true },
group: String,
created_at: Date,
updated_at: Date
});
var User = mongoose.model(
'User',
userSchema
);
module.exports = User;
带有能够创建用户的表单的索引页面。如果保存无法正常工作,我想向用户显示flash消息。我知道这样做的前提是在get
路由中为消息创建一个位置,如果消息存在,则呈现包含该消息的dom元素。不幸的是,我所能管理的只是崩溃服务器并收到:
events.js:160
throw er; // Unhandled 'error' event
^
BulkWriteError: E11000 duplicate key error collection: test.users index: email_1 dup key: { : "test@gmail.com" }
at /home/bob/sites/mysite.ca/projects/login/node_modules/mongodb/lib/bulk/unordered.js:528:15
at handleCallback (/home/bob/sites/mysite.ca/projects/login/node_modules/mongodb/lib/utils.js:128:55)
at resultHandler (/home/bob/sites/mysite.ca/projects/login/node_modules/mongodb/lib/bulk/unordered.js:454:5)
at /home/bob/sites/mysite.ca/projects/login/node_modules/mongodb-core/lib/connection/pool.js:541:18
at _combinedTickCallback (internal/process/next_tick.js:73:7)
at process._tickCallback (internal/process/next_tick.js:104:9)
var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log('index.js db connected!');
});
var User = require('../schemas/user');
router.post('/create', function(req, res, next) {
var user = new User({
username: req.body.username,
email: req.body.email,
password: req.body.password
});
user.save(function(error) {
if (error) console.log(error);
console.log('User ' + user.username + ' successfully created!');
});
});
/* GET home page. */
router.get('/', function(req, res, next) {
res.render(
'index',
{
title: 'Express'
}
);
});
module.exports = router;
如何捕获此错误并处理它而不是崩溃节点服务器?
答案 0 :(得分:1)
我认为您的代码是正确的,您只需要在app.js
中创建一次mongoose连接,默认情况下,mongoose使用5个并发连接的池,并且不需要在每个路由器中创建连接: / p>
<强> app.js 强>
...
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
...
<强>路由/ index.js 强>
您必须向客户端发送响应以终止请求 - 响应周期,否则客户端将被挂起:
var express = require('express');
var router = express.Router();
var User = require('../schemas/user');
...
router.post('/create', function(req, res, next) {
var user = new User(req.body);
user.save(function(err, user) {
if (err) return res.json(err);
res.send('User ' + user.username + ' successfully created!');
});
});
答案 1 :(得分:0)
在用户模型中,您将用户名和电子邮件设置为唯一类型,并且错误表明该值包含&#34; test@gmail.com" 重复(已存在),因为其类型是唯一的。 因此,请检查您的帖子数据是否重复,然后重置您的数据库,然后尝试。
尝试这种方法我认为它应该可以防止你的节点崩溃:
router.post('/create', async(req, res, next) => {
try {
var user = new User({
username: req.body.username,
email: req.body.email,
password: req.body.password
});
var created = await user.save();
res.send({ result: created })
} catch (error) {
console.log('error', error);
res.send({ error });
}
});
答案 2 :(得分:0)
我更喜欢匹配异常名称和代码以识别错误类型
try {
var user = new User({
username: req.body.username,
email: req.body.email,
password: req.body.password
});
var created = await user.save();
res.send({ result: created })
} catch (exception) {
if (exception.name === 'MongoError' && exception.code === 11000) {
res.send({ exception });
}
}