// Route for creating a new Review and updating Product "review" field with it app.post("/product/:id", function(req, res) { // Create a new note and pass the req.body to the entry db.Review.create(req.body) .then(function(dbReview) { // If a Review was created successfully, find one Product with an `_id` equal to `req.params.id`. Update the Product to be associated with the new Review // { new: true } tells the query that we want it to return the updated Product -- it returns the original by default // Since our mongoose query returns a promise, we can chain another `.then` which receives the result of the query return db.Product.findOneAndUpdate({ _id: req.params.id }, { review: dbReview._id }, { new: true }); }) .then(function(dbProduct) { // If we were able to successfully update a Product, send it back to the client res.json(dbProduct); }) .catch(function(err) { // If an error occurred, send it to the client res.json(err); }); }); |