Javascript node.js多个app.param()
我试着为不同的参数使用两个不同的app.param,但似乎不可能。当服务器检查第一个参数时,我让它依次检查第二个参数,即使它在第二个app.param中检查第二个参数,它也会使第一个参数无效Javascript node.js多个app.param(),javascript,node.js,express,Javascript,Node.js,Express,我试着为不同的参数使用两个不同的app.param,但似乎不可能。当服务器检查第一个参数时,我让它依次检查第二个参数,即使它在第二个app.param中检查第二个参数,它也会使第一个参数无效 app.param('chatroom', function(req, res, next, chatroom) { Chatroom.findOne({ 'chat.roomname' : chatroom }, function(err, chatroom) { if(err)
app.param('chatroom', function(req, res, next, chatroom) {
Chatroom.findOne({ 'chat.roomname' : chatroom }, function(err, chatroom) {
if(err)
throw err;
if(!chatroom)
return next();
req.chatroom = chatroom;
next();
});
});
app.get('/:chatroom', function(req, res) {
Profile.findOne({ 'pic.username' : req.user.local.username}, function(err, profilepic) {
if(err)
throw err;
res.render('pages/chatroom', {
chatroom : req.chatroom,
profilepic : profilepic,
user : req.user
});
});
});
app.param('username', function(req, res, next, username) {
User.findOne({ 'local.username' : username }, function(err, user) {
if(err)
throw err;
if(!user){
return res.sendStatus(404);
}
req. profile = user;
next();
});
});
app.get('/:username', function(req, res) {
async.parallel([
function(callback) {
Profile.findOne({ 'pic.username' : req. profile.local.username }, function(err, profilepic) {
if(err)
throw err;
callback(null, profilepic);
});
}, function(callback) {
Buddy.find({ 'bud.username' : req. profile.local.username }, function(err, buddy) {
if(err)
throw err;
callback(null, buddy);
});
}, function(callback) {
Buddy.count({ 'bud.username' : req. profile.local.username}, function(err, buddycount) {
if(err)
throw err;
callback(null, buddycount);
});
}, function(callback){
Userpost.find({ 'post.postuser' : req. profile.local.username }).sort({ 'post.date' : 1 }).exec(function(err, userpost) {
if(err)
throw err;
callback(null, userpost);
});
}, function(callback) {
Userpost.count({ 'post.postuser' : req. profile.local.username }, function(err,postcount) {
if(err)
throw err;
callback(null, postcount);
});
}], function(err, results){
if(err)
throw err;
var profilepic = results[0];
var buddy = results[1];
var buddycount = results[2];
var userpost = results[3];
var postcount = results[4];
res.render('pages/visitprofile', {
user : req. profile,
profilepic : profilepic,
buddy : buddy,
buddycount : buddycount,
userpost : userpost,
postcount : postcount
});
}
);
});
那么,有没有办法让这两个参数同时在服务器上工作呢。我对它们重新排序,其中一个有效,另一个无效。也许我可以让next()函数跳过另一个参数“怎么做?”这就是问题所在,不管怎样,它都会转到下一个部分,即另一个app.param。有没有办法用next()函数跳过app.param和app.get('/:chatroom')?app.get('/:chatroom')、…和
app.get('/:username'
)没有next
?是的,我知道了。只是做了if语句if(req.profile)返回next();按以下路线