我是Node.js的新手,我在尝试避免回调地狱时遇到了问题。 现在我正在开发一个endpoitn来在mongo数据库中注册用户,我所面临的问题是,如果我将使用它们的函数模块化,我将失去对某些变量的访问。
代码:
工作回调地狱:
var mongoose = require('mongoose'),
dbserver = 'mongodb://localhost/',
dbname = 'authdb';
mongoose.connect(dbserver + dbname);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback () {
console.log('connectet to mongo db: ' + dbname);
})
userModel = require('./user.js');
function singIn (req, res){
var email = req.body.email,
name = req.body.name,
secondName = req.body.secondName,
pass = req.body.pass,
bday = req.body.bday;
var user = new userModel.user();
user.Email = email;
user.Name = name;
user.SecondName = secondName;
user.Pass.Hash = pass;
user.BirthDate = bday;
db.model('user').find({Email : email},function (err,foundUsers){
if(!err && foundUsers.length === 0){
user.save(function (err, users){
if(err){
console.log(err);
res.send("Not inserted");
}else{
console.log(user);
console.log('registered!');
res.send("OKish");
}
});
}else{
console.log(err);
console.log(foundUsers);
res.send("Not inserted");
}
});
};
exports.singIn = singIn;
不工作模块化
var mongoose = require('mongoose'),
dbserver = 'mongodb://localhost/',
dbname = 'authdb';
mongoose.connect(dbserver + dbname);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback () {
console.log('connectet to mongo db: ' + dbname);
})
userModel = require('./user.js');
function singIn (req, res){
var email = req.body.email,
name = req.body.name,
secondName = req.body.secondName,
pass = req.body.pass,
bday = req.body.bday;
var user = new userModel.user();
user.Email = email;
user.Name = name;
user.SecondName = secondName;
user.Pass.Hash = pass;
user.BirthDate = bday;
db.model('user').find({Email : email},addUser);
};
function saveUser(err, users){
if(err){
console.log(err);
res.send("Not inserted");
}else{
console.log(user);
console.log('registered!');
res.send("OKish");
}
}
function addUser(err,foundUsers){
if(!err && foundUsers.length === 0){
user.save(SaveUser);
}else{
console.log(err);
console.log(foundUsers);
res.send("Not inserted");
}
}
exports.singIn = singIn;
这里的问题是,在第二个片段中,我无法访问函数addUser和saveUser中的变量res和user。
有没有办法让这两个变量都在函数范围内,或者这次我必须承担回调地狱?
谢谢!
答案 0 :(得分:1)
您可以在上下文saveUser
中定义addUser
和signIn
函数,以便他们可以访问其参数和局部变量:
function signIn (req, res){
var email = req.body.email,
name = req.body.name,
secondName = req.body.secondName,
pass = req.body.pass,
bday = req.body.bday;
var user = new userModel.user();
user.Email = email;
user.Name = name;
user.SecondName = secondName;
user.Pass.Hash = pass;
user.BirthDate = bday;
db.model('user').find({Email : email},addUser);
function saveUser(err, users){
if(err){
console.log(err);
res.send("Not inserted");
}else{
console.log(user);
console.log('registered!');
res.send("OKish");
}
}
function addUser(err,foundUsers){
if(!err && foundUsers.length === 0){
user.save(saveUser);
}else{
console.log(err);
console.log(foundUsers);
res.send("Not inserted");
}
}
};
答案 1 :(得分:0)
要么使用回调,要么将任何范围内变量作为额外参数传递。
答案 2 :(得分:0)
您可以将第一个参数添加到例如saveUser
并将其绑定到res:
function saveUser(res, err, users){
if(err){
console.log(err);
res.send("Not inserted");
}else{
console.log(user);
console.log('registered!');
res.send("OKish");
}
}
然后
user.save(saveUser.bind(null,res));