Skip to content

Commit a61347b

Browse files
committed
added populate method
1 parent 6f14b7a commit a61347b

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

app.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ app.use(helmet());
1515

1616
//Set up mongoose connection
1717
var mongoose = require("mongoose");
18-
var mongoDB =
18+
var dev_db_url =
1919
"mongodb+srv://beautifulchaos:[email protected]/inventory?retryWrites=true&w=majority";
20+
var mongoDB = process.env.MONGODB_URI || dev_db_url;
2021
mongoose.connect(mongoDB, { useNewUrlParser: true, useUnifiedTopology: true });
2122
var db = mongoose.connection;
2223
db.on("error", console.error.bind(console, "MongoDB connection error:"));

controllers/itemController.js

+14-12
Original file line numberDiff line numberDiff line change
@@ -255,18 +255,20 @@ exports.item_delete_get = function (req, res, next) {
255255
};
256256

257257
exports.item_delete_post = function (req, res, next) {
258-
Item.findById(req.params.id).exec(function (err, result) {
259-
if (err) return next(err);
258+
Item.findById(req.params.id)
259+
.populate("brand category")
260+
.exec(function (err, result) {
261+
if (err) return next(err);
260262

261-
if (req.body.name === result.name) {
262-
Item.findByIdAndDelete(req.params.id, function (err) {
263-
if (err) return next(err);
263+
if (req.body.name === result.name) {
264+
Item.findByIdAndDelete(req.params.id, function (err) {
265+
if (err) return next(err);
264266

265-
res.redirect("/items");
266-
});
267-
return;
268-
}
269-
270-
res.render("item_delete", { item: result, error: "Name does not match" });
271-
});
267+
res.redirect("/items");
268+
});
269+
return;
270+
}
271+
272+
res.render("item_delete", { item: result, error: "Name does not match" });
273+
});
272274
};

0 commit comments

Comments
 (0)