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 8797905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:57:10+00:00 2026-06-13T23:57:10+00:00

What i am trying to do is the write a javascript function to access

  • 0

What i am trying to do is the write a javascript function to access a defined articles schema in my .js file.

I have already determined that the below queries work in the mongodb terminal:

    db.articles.ensureIndex( { "comments.user_id" : 1 } )
        db.articles.find( { "comments.user_id" : 987654 } ) // returns all document fields, meaning X and Y including comments

        db.articles.find( { "comments.user_id" : 987654 }, 
{ "title" : 1, "comments.user_id" : 1 })   //some trimming

The purpose of the javascript function is to retrieve all comments made by a specific user, is my below attempt correct corresponding to the above mongodb queries? Are the style, syntax considered good practice?

exports.allCommentsByUser = function(userId){ 
   db.articles.ensureIndex({"comments.user_id" : 1})
    var allComments = db.articles.find({"comments.user_id" : userId}, 
                  { "title" : 1, "comments.user_id" : 1 });
    return allComments;
}

Q: Further, how do i convert the above javascript function to a closure function?

Note: i am using mongoose as a wrapper

  • 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-06-13T23:57:12+00:00Added an answer on June 13, 2026 at 11:57 pm

    That won’t work because allComments is a Mongoose Query object, not the results. You need to add a callback parameter to your allCommentsByUser method that the method will use to provide the results back to the caller once the async find call completes.

    exports.allCommentsByUser = function(userId, callback){ 
        db.articles.find(
            {"comments.user_id" : userId}, 
            { "title" : 1, "comments.user_id" : 1 }, 
            callback);
    };
    

    Usage of the method:

    x.allCommentsByUser(userId, function (err, articles) {
        if (err) {
            console.error(err);
        } else {
            console.log(articles);
        }
    });
    

    Not sure what you’re asking in your second question regarding a ‘closure function’.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a javascript function that adds some DOM nodes to the
I am trying to write a javascript function with an if statement that will
I am trying to write a javascript function that calls an Dictionary value and
I'm trying to write a Javascript regexp replace function that allows me to replace
I am trying to write a Javascript function that will be called whenever a
I'm trying to write a javascript function that calculates the time since Oct 11th,
I have a JavaScript function that I am trying to call when the confirm
I am trying to write a javascript function that when called performs function DoSomething()
I am trying to write a JavaScript function that will expand an image to
I am trying to write a JavaScript function that will return its first argument(function)

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.