Skip to content

Instantly share code, notes, and snippets.

@mkozhukharenko
Created November 6, 2016 14:01
Show Gist options
  • Save mkozhukharenko/e3dffb41b12979b14de93ca0318bbdad to your computer and use it in GitHub Desktop.
Save mkozhukharenko/e3dffb41b12979b14de93ca0318bbdad to your computer and use it in GitHub Desktop.
var co = require('co')
app.post('/',function (req, res, next) {
var body = req.body;
if (!body.name || !body.email || !body.password) {
return res.status(400).send("Missing a username or email or password")
};
co(function *(){
var user = yield User.findOneAndUpdate({
email: body.email
}, {
$set: {password: body.password}
});
if (!user) {
user = yield User.create(body);
}
res.send({ user: body })
}).catch((err) => {
if (err && err.name === 'ValidationError') {
return res.status(400).send(err)
} else if (err) {
return res.status(500).send(err)
}
res.send({ user: user })
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment