Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6854559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:38:54+00:00 2026-05-27T01:38:54+00:00

In a node.js / Mongoose project, I have a schema which contains references to

  • 0

In a node.js / Mongoose project, I have a schema which contains references to external image files.

var PageSchema = new Schema({
    title: String
  , media: {
        digest: String
      , name: String
    }
});

Those files have additional properties which are stored in the file itself: url, width, height, exif fields, etc. Those fields will need to be populated before the model being sent to res.render().

For some fields, things are synchronous and a virtual just does the job:

PageSchema.virtual('media.url').get(function () {
    return appPaths.fileUrl(this.media);
});

However, width / height, or exif fields require async calls. I thought of using middleware to populate them, but this does not seem to work:

PageSchema.post('init', function(next) {
    var media = this.media;
    var fileName = filedb.absoluteFilePath(media);

    im.identify(fileName, function(err, features) {
        if (err) {
            media.width = 0;
            media.height = 0;
        } else {
            media.width = features.width;
            media.height = features.height;
        }

        next();
    });
});

What am I doing wrong? Is there a common design pattern for solving this kind of problem? (Other than duplicating this information in the database itself?)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:38:55+00:00Added an answer on May 27, 2026 at 1:38 am

    The real problem here is that mongoose currently seems to have a wonky implementation of post callbacks. While pre('init',function(next){ ... }); works as you expect, post('init',function(next){ ... }); does not actually get passed a next function. In fact, the post init callback does not receive any arguments whatsoever when it is called.

    As such, I usually write a wrapper for my query callbacks to make a sort of DIY middleware:

    var setAsyncVirtuals = function(callback){
      return function(err, docs){
        if(err) return callback(err);
        var i = done = docs.length;
        if(i > 0)
          while(i--){
            (function(i){
              var filename = getFilename();
              im.identify(filename, function(err, features) {
                if (err) {
                  docs[i].media.width = 0;
                  docs[i].media.height = 0;
                } else {
                  docs[i].media.width = features.width;
                  docs[i].media.height = features.height;
                }
                done--;
                if(done <= 0) callback(null, docs);
              });
            })(i); // bind i to hold value for async call
          }
        else callback(null, docs);
      }
    }
    

    then

    Page.find({}, setAsyncVirtuals(function(err,docs){
      res.send(docs); // these have media.width & media.height assigned
    }));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Based on this example (which works): var Comment = new Schema(); Comment.add({ title :
I have a mongoose model in my node.js application, representing invoices. I have figured
I am trying to figure out how to use mongoose-joins for Node.js. I have
I am using Node.js and Mongoose to store some data. After update, I have
I'm building a node.js application with Mongoose and have a problem related to sorting
I am new to Node, and I'm using Mongoose as a driver for MongoDB.
I am using now.js and Mongoose in a node project and am having trouble
I'm using Mongoose with Node.js and have the following code that will call the
Hey all, I'm very new to mongodb, mongoose and node.js. I would like to
I have a node with many CCK fields. I want to hide a field

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.