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

The Archive Base Latest Questions

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

I’m following this tutorial about building a blog with node.js, express and mongodb. The

  • 0

I’m following this tutorial about building a blog with node.js, express and mongodb.

The following are functions that control articles submission (I think).

Of providers and data

Because the intention of this article is to show how one might use a
persistent approach in node.js we shall start with an abstraction:
provider. These ‘providers’ are going to responsible for returning and
updating the data. Initially we’ll create a dummy in-memory version
just to bootstrap us up and running, but then we’ll move over to using
a real persistence layer without changing the calling code.

articleprovider-memory.js:

var articleCounter = 1;

ArticleProvider = function(){};
ArticleProvider.prototype.dummyData = [];

ArticleProvider.prototype.findAll = function(callback) {
  callback( null, this.dummyData )
};

ArticleProvider.prototype.findById = function(id, callback) {
  var result = null;
  for(var i =0;i<this.dummyData.length;i++) {
    if( this.dummyData[i]._id == id ) {
      result = this.dummyData[i];
      break;
    }
  }
  callback(null, result);
};

ArticleProvider.prototype.save = function(articles, callback) {
  var article = null;

  if( typeof(articles.length)=="undefined")
    articles = [articles];

  for( var i =0;i< articles.length;i++ ) {
    article = articles[i];
    article._id = articleCounter++;
    article.created_at = new Date();

    if( article.comments === undefined )
      article.comments = [];

    for(var j =0;j< article.comments.length; j++) {
      article.comments[j].created_at = new Date();
    }

Can anyone explain to me in simple English what each function is doing (sorry I’m a JavaScript beginner)?

(by the way, it is because of any common practice that the author decided to only start with a CAP in ArticleProvider?)

  • 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:33:23+00:00Added an answer on May 27, 2026 at 1:33 am

    This file is a class definition for ArticleProvider with various instance methods.

    1. Defining article provider as function and then using prototype to define further functions
      of ‘findAll’, ‘findById’, and ‘save’, means that you can call these functions using the
      syntax:

      foo = new ArticleProvider();
      foo.findAll(callback);
      foo.findById(id, callback);
      foo.save(articles, callback);
      
    2. articleCounter is a variable locally available to the file containing
      ArticleProvider definition

    3. dummyData is an internal variable available to an ArticleProvider object

    4. ArticleProvider.findAll(callback)

      Will invoke callback(array) with all articles currently saved to ArticleProvider.
      Caller must have defined a callback function that can accept one variable, and expect
      the variable to be filled with an array of articles when called

    5. ArticleProvider.findById(id, callback)

      Will invoke callback(article) with a single article that matches the ‘id’ provided
      in the parameter. Again, callback is defined by caller and accepts a single parameter
      which will be the article found

    6. ArticleProvider.save(articles,callback)

      What you’ve listed here is truncated so I only have a partial answer for this function:

      Will accept an array of articles, and set various fields in the submitted articles. These
      fields include the article id, article creation date, article comments, and comment creation
      dates

      I’m guessing that in the code not listed these articles are saved to
      ArticleProvider.dummyData, and that the callback is called with some parameter.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build

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.