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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:03:27+00:00 2026-05-24T13:03:27+00:00

Suppose I have a CMS application written in Node.js which persists data on a

  • 0

Suppose I have a CMS application written in Node.js which persists data on a Redis database. When this application creates a new content, it should increment the id counter, add the new id to a list of ides and then set a new hash with content. What I would do for now is to create a function to perform this execution. This function (let us call it createArticle()) would have a callback and would execute the increment. Once the increment was executed, a callback function would push it into the list of ids. After that, another callback would create the hash. The hash-creating callback would call the function passed as parameter to createArticle():

function createArticle(title, content, callback) {
    var client = redis.createClient();
    client.incr("idCounter", function(err, id) {
        if (err) return callback(err, data);
        client.lpush("articleIds", id, function (err, data) {
            if (err) return callback(err, data);
            var key = "article:"+id;
            client.hmset(key, "title", title, "content", content, callback);
        });
    });
}

I would use this function more or less this way (using Express in this example):

app.post('/createarticle', function(req, res) {
    var title = req.body.article.title, 
        content = req.body.article.content;
    createArticle(title, content, function(err, data) {
        if (err) return res.render('error', { status: 500, message: 'Internal Server Error' });
        res.render('index', { status: 200, message: 'Article created!' });
    });
});

However, this code looks a bit cumbersome to me. Is this the way to go. Or is there a better way to do a series of I/O steps? I used Express and Redis in my example, but the answer do not need to use them.

  • 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-24T13:03:27+00:00Added an answer on May 24, 2026 at 1:03 pm

    You can make those error-catchers single-lined:

    function createArticle(title, content, callback) {
        var client = redis.createClient()
        client.incr("idCounter", function(err, id) {
            if (err) return callback(err, data)
            client.lpush("articleIds", id, function (err, data) {
                if (err) return callback(err, data)
                var key = "article:"+id
                client.hmset(key, "title", title, "content", content", callback)
            })
        })
    }
    

    And you could use a helper for handling errors:

    function noError(errorCb, cb) {
        var slice = Array.prototype.slice
        return function (err) {
            var currentCb = err ? errorCb : cb
            currentCb.apply(this, slice.apply(arguments, err?0:1)
        }
    }
    
    function createArticle(title, content, cb) {
        var client = redis.createClient()
        client.incr("idCounter", noError(cb, function(id) {
            client.lpush("articleIds", id, noError(function (data) {
                var key = "article:"+id
                client.hmset(key, "title", title, "content", content", callback)
            }))
        })
    }
    

    Or something like that.

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

Sidebar

Related Questions

Suppose I have a stringbuilder in C# that does this: StringBuilder sb = new
Let's suppose that we have multi-site CMS and every website in this CMS having
Suppose I have logged into an application which is running from IIS . Now
Suppose I have an SQL table entitled Facility which looks like this: || FacilityID
Suppose I have two applications written in C#. The first is a third party
Suppose I have to put x.com CMS app to Japan, Mexico, US, Australia, and
Suppose you have an old legacy website, with an in-house php CMS. http://mysite.com/index.php?page=3&c=7 Now,
I have _Layout.cshtml defined for my mvc application, which is shown below: @inherits System.Web.Mvc.WebViewPage
Suppose I have a macro defined as this: #define FOO(x,y) \ do { int
Suppose I have this code: class Num: def __init__(self,num): self.n = num def getn(self):

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.