Created
November 6, 2016 14:01
-
-
Save mkozhukharenko/e3dffb41b12979b14de93ca0318bbdad to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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