1

I have a Product model with ref to a Category model and the following code works fine but was wondering if this is the best way of doing it.

server.get "/:id/edit", (req, res) ->
    Category.find({})
    .sort("position", 1)
    .exec (err, categories) ->
      if not err
        Product.findOne({_id: req.params.id })
        .populate("categories")
        .exec (err, data) ->
          if err
            res.json err
          else
            res.render "#{view_path}/products/edit",
              title: "Edit Product"
              user: req.session.user
              product: data
              categories: categories

1 Answers1

0

How useful using populate is greatly depends on how complex your Category model is and what you're using it for on the Product model related pages.

Generally speaking categories are a great example of something to be embedded into your products since they will change infrequently and will be pulled with the product all the time.

You may find this question interesting. There is always quite an ongoing discussion about use cases for references or embedding.

Community
  • 1
  • 1
Dan
  • 1,721
  • 1
  • 9
  • 7