Skip to content

Instantly share code, notes, and snippets.

@carlosmaniero
Created February 19, 2019 23:57
Show Gist options
  • Save carlosmaniero/871ab6436db750032867b58e43cda6e7 to your computer and use it in GitHub Desktop.
Save carlosmaniero/871ab6436db750032867b58e43cda6e7 to your computer and use it in GitHub Desktop.
app.post('/customers/:customerId/orders/', (req, res) => {
if (req.body.items.length === 0) {
res.status(400).end()
} else {
MongoClient.connect(url, function (err, client) {
if (err) {
console.error(err)
}
const db = client.db(dbName)
const collection = db.collection('orders')
const order = req.body
order.customerId = req.params.customerId
collection.insertOne(order, function (err, result) {
if (err) {
console.error(err)
}
res.send(result.ops[0])
})
client.close()
})
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment