i want to insert data in mongodb after all promises have been done by using node.js
<p>i am pushing errors in array by using promises but before it push errors in array it check the condition for insert data like in else i am inserting data.</p>

<pre><code>var errorsArr= [];

var username = new Promise(function(resolve,reject){
    User.findOne({ "username": req.body.username },function(err,user){
        if(err)
            reject(err);

        if(user) {
            resolve(1);
        }else{
            resolve(0);
        }
    });
});
username.then(function(data){
    if( data == 1 )
        errorsArr.push({"msg": "Username already been taken."});
}).catch(function(err){
    console.log(err);
});

var email = new Promise(function(resolve,reject){
    User.findOne({ "email": req.body.email },function(err,user){
        if(err)
            reject(err);

        if(user) {
            resolve(1);
        }else{
            resolve(0);
        }
    });
});
email.then(function(data){
    if( data == 1 )
        errorsArr.push({"msg": "email already been taken."});
}).catch(function(err){
    console.log(err);
});

if(errorsArr.length &gt;0)
{
    req.session.error = errorsArr;
    return  res.redirect('/auth/Registration');
}
else {
    var newUser = new User();
    newUser.username = req.body.username;
    newUser.password = req.body.password;
    newUser.sex = req.body.sex;
    newUser.email = req.body.email;
    newUser.phoneNumber = req.body.phoneNumber;
    newUser.age = req.body.age;
    newUser.designation = req.body.designation;
    newUser.timing = req.body.timing;
    var CurrentDate = moment.tz(new Date(req.body.joiningDate), "Asia/Karachi").unix();
    newUser.joiningDate = CurrentDate;
    newUser.save(function (err, user) {
        if (!err)
            return res.redirect('/auth/Login');
    });
}
</code></pre>

<p>can you please help me to do it in better way i am new in node.js. Thanks in advance.</p>