Created
September 28, 2015 21:07
-
-
Save chrisckchang/a3c1c3057f51f9f4e45f to your computer and use it in GitHub Desktop.
Contact list /contacts POST request
This file contains hidden or 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
app.post("/contacts", function(req, res) { | |
var newDoc = req.body; | |
newDoc.createDate = new Date(); | |
if (req.body.firstName === undefined || req.body.lastName === undefined) { | |
handleError(null, res, "Must provide a contact name."); | |
} | |
if (req.body.email === undefined) { | |
handleError(null, res, "Must provide an email."); | |
} | |
db.collection("contacts").insertOne(newDoc, function(err, doc) { | |
if (err) { | |
handleError(err, res, "Failed to create new contact."); | |
} else { | |
res.status(201).json(doc.ops[0]); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment