有没有可能在Node上找到一个与猫鼬有关的东西?
我让用户更改他们的电子邮件地址,但我必须确保电子邮件没有被任何其他用户使用之前,我保存新的电子邮件。
我想这么做:
/*************************************************************
* MY_USER - PROFILE UPDATE
*************************************************************/
app.put('/api/myuser/info', auth, function(req, res) {
serverLog.log(req, production, {});
// User ID
var myUserID = req.session.passport.user._id;
if ( myUserID && validateID(myUserID) ) {
User.findOne({
_id: myUserID
}, function(err, data) {
if (err) throw err;
if (data == null) {
res.sendStatus(401);
console.log(401);
}
// Data
else {
// Update Email
if (req.body.email) {
// Check valid email
if ( validateEmail(req.body.email) ) {
console.log('validateEmail');
// Check Unique Email
User.findOne({
'local.user.info.email': email
}, function(err, user) {
if(err) return err;
if ( user ) {
// Email already in use
res.status(400).send('ERROR: Email Already in Use');
return;
}
console.log('uniqueEmail ' + true);
// Update email
user.local.user.info.email = req.body.email;
})
}
// Bad Email
else {
res.status(400).send('ERROR: Not a propper Email');
return;
}
}
// SAVE USER DATA
if ( info_sent ) {
user.save(function(err, data) {
if (err) throw err;
res.setHeader('Content-Type', 'application/json');
res.status(200).send(JSON.stringify(data.local.user.info, null, 3));
return;
});
}
// NO INFO WAS SENT
else {
res.status(400).send('ERROR: No information was sent.');
return;
}
}
});
}
// Bad / No User ID
else {
res.sendStatus(401);
}
});我找到了用户,然后检查电子邮件是否在使用中,你会怎么做呢?
发布于 2017-10-13 23:38:40
这不起作用,因为您没有将您的用户保存在回调函数中,该函数检查电子邮件是否已经存在。也要考虑用承诺来避免地狱的回调
不管怎样,你可以这样做:
// Check Unique Email
User.findOne({'local.user.info.email': email }, (err, user) => {
if (err) throw err;
if (user) {
return res.status(400).send('ERROR: Email Already in Use');
} else { // SAVE USER DATA
if (info_sent) {
user.save((err, data) => {
if (err) throw err;
res.setHeader('Content-Type', 'application/json');
return res.status(200).send(JSON.stringify(data.local.user.info, null, 3));
});
} else {
return res.status(400).send('ERROR: No information was sent.');
}
}https://stackoverflow.com/questions/46720951
复制相似问题