如何在发送响应后停止nodejs ajax请求代码

时间:2014-11-08 07:46:42

标签: node.js mongodb express monk

我在Nodejs中实现了一个简单的用户注册表单。我正在使用Express框架,Mongodb和Monk。我成功地实现了正常的数据库插入。但现在,当我添加代码以首先检查输入的电子邮件或用户名是否存在时,它已经变得混乱了。问题是,当找到具有相同电子邮件或用户名的条目时,我使用res.end发送错误响应,但代码不会停在那里并继续执行其他条件并最终插入文档而不管条件,有时会给出错误can't set headers after they are sent。下面是我的ajaxsignup路线的代码

router.post('/ajaxsignup', function(req, res){
var db = req.db;
var formdata = req.body;

var collection = db.get('userscollection');

res.setHeader('Content-Type', 'application/json');

var emailExists = collection.findOne({"email": formdata.email}, function(err, doc){
    if(err)
    {
        response = {'status': 0, 'err_code': err.code, 'err_msg': err.err};
        res.end(JSON.stringify(response));
    }
    else
    {
        if(doc)
        {
            response = {'status': 0, 'err_code': 0, 'err_msg': 'This email id is already registered. Please use a different one.'};             
            res.end(JSON.stringify(response));
        }
    }
});//emailExists

var usernameExists = collection.findOne({"username": formdata.username}, function(err, doc){
    if(err)
    {
        response = {'status': 0, 'err_code': err.code, 'err_msg': err.err};
        res.end(JSON.stringify(response));
    }
    else
    {
        if(doc)
        {
            response = {'status': 0, 'err_code': 0, 'err_msg': 'This username is already in use. Please use a different one.'};             
            res.end(JSON.stringify(response));
        }
    }
});//usernameExists



formdata.created = new Date();
formdata.last_updated = formdata.created;

collection.insert( formdata, function(err, doc){
    if(err)
    {
        response = {'status': 0, 'err_code': err.code, 'err_msg': err.err};
    }
    else
    {
        response = {'status': 1, 'msg': 'Thank you. You can proceed to login now.'};
    }

    res.end(JSON.stringify(response));
});//insert
});//ajaxsignup

1 个答案:

答案 0 :(得分:0)

发送后无法设置标头这个错误是因为你写了三次res.end 有几种方法可以做到这一点

1)使mongo电子邮件和用户名唯一,当您尝试插入已存在的电子邮件或用户名时,它将返回错误

new Schema{
    email:{type:String,index: { unique: true },required:true}
    username:{type:String,index: { unique: true },required:true}
}






collection.insert(formdata,function(err,data){
       if(err){
         res.json({success:false,text:"User with this email or username already   exists!"}); 
       }
       else{
         res.json({success:true,text:"Your registration was successfull"}); 
       }
    })

2)第二是你的方式

var emailExists=false,usernameExists=false;
collection.findOne({"$or":[{"email": formdata.email},{"username":formdata.username}], function(err, doc){
    if(err)
    {
        response = {'status': 0, 'err_code': err.code, 'err_msg': err.err};
        res.end(JSON.stringify(response));
    }
    else
    {
        if(!doc)
        {
         collection.insert( formdata, function(err, doc){
    if(err)
    {
        response = {'status': 0, 'err_code': err.code, 'err_msg': err.err};
    }
    else
    {
        response = {'status': 1, 'msg': 'Thank you. You can proceed to login now.'};
    }

    res.end(JSON.stringify(response));
});//insert
});  
        }else{
           response = {'status': 0, 'err_code': 0, 'err_msg': 'This email id is already registered. Please use a different one.'};             
            res.end(JSON.stringify(response));
        }
    }
});