NodeJs - how to populate the model view with data from another model

28
June 10, 2021, at 06:50 AM

I am practicing Node by making a simple inventory app, in which I have 3 models: Item, Category and Manufacturer. What I want to do is, when displaying an item details, to include the Category and Manufacturer associated with that specific item, however it seems that I can not make it work. Any help would be appreciated.

Here is the Item controller

const Item = require("../models/item");
const Category = require("../models/category");
const Manufacturer = require("../models/manufacturer");
const async = require("async");
exports.index = function (req, res) {
  res.render("index", { title: "StrinGuist" });
};
//display list of all items
exports.item_list = function (req, res, next) {
  Item.find({}, "name description category in_stock price manufacturer")
    .populate("item")
    .exec(function (err, list_items) {
      if (err) {
        return next(err);
      }
      res.render("item_list", { title: "All items", item_list: list_items });
    });
};
//display detail page for a specific item
exports.item_detail = function (req, res, next) {
  async.parallel(
    {
      item: function (callback) {
        Item.findById(req.params.id)
          .populate("category")
          .populate("manufacturer")
          .exec(callback);
      },
    },
    function (err, results) {
      if (err) {
        return next(err);
      }
      if (results.item == null) {
        // No results.
        const err = new Error("Item not found");
        err.status = 404;
        return next(err);
      }
      // Successful, so render.
      res.render("item_detail", {
        title: results.item.name,
        item: results.item,
      });
    }
  );
};

and here is the view (pug)

extends layout 
block content 
  h1 #{item.name}
  div(class='item-detail-content')
    p #[strong Price: $] #{item.price}
    p #[strong Description: ] #{item.description}
    p #[strong In stock: ] #{item.in_stock}
    p #[strong Manufacturer: ] #{item.manufacturer.name}
    p #[strong Category: ] #{item.category.name}

This throws me an error "Cannot read property 'name' of null", but I am not quite sure what is wrong. I tried also to declare Category and Manufacturer name as 'virtual', but that didn't work either.

Answer 1

For a ORM like mongoose when using mongodb, you can use the populate method with allows you do that.

https://mongoosejs.com/docs/populate.html

READ ALSO
Date of Birth does not show and Form cannot edit in PHP

Date of Birth does not show and Form cannot edit in PHP

I will describe my problems brieflyThere are 2 main issues in my web app:

12
AWS Lambda: Input values needs to be concatenated with error message

AWS Lambda: Input values needs to be concatenated with error message

The query returns the expected result, the request in this forum is regarding the customized error messageIncase of failure the exception needs to stored along with the input values

50
Aggregated query returning 1s where 0's are expected

Aggregated query returning 1s where 0's are expected

In my devices table, is_muted is 0 for all 4 rows:

52
Parse an HTML string with JS

Parse an HTML string with JS

I searched for a solution but nothing was relevant, so here is my problem:

35