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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:46:57+00:00 2026-05-30T01:46:57+00:00

I have been using node and redis for some time. The problem I am

  • 0

I have been using node and redis for some time. The problem I am experiencing is, when I use hgetall in redis, it returns an object.

 { uid: '6203453597',
    first_name: 'Name',
    last_name: 'Surname',
    gender: 'male',
    email: 'email@email.com',
    status: '1',
    chips: '4002043' } 

However, when I use hmget and specify the fields I want to get, it returns an array.

[ '6203453597', 'Name', 'Surname', '4002043' ]

So, I would like to convert array to an associative array, just like the first one. What is the best way to convert it from code and performance wise.

I am also using the multi command in redis. So it returns an array of objects in the first example, in the second example it return an array of arrays. So, it is important it to be efficient and automatic, not manual.

YUI’s dataschema function is what I am looking for. However it needs to be done on node.js and the only 3rd party utility tool I am using is underscore. Is there any easy way of doing this, or do I need to convert hem in a loop, manually.

Thanks,

  • 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-30T01:46:58+00:00Added an answer on May 30, 2026 at 1:46 am

    I have built something similar to what you want, but for the sort command. This should work for hmget:

    function getFieldsAsObject(key, fields, next) {
        function mapResults(err, values) {
            if(err)
                return next(err);
    
            var result = {};
            for(var i = 0, len = fields.length; i < len; i++) {
                result[fields[i]] = values[i];
            }
            next(null, result);
        }
    
        var args = [].concat(key).concat(fields).concat(mapResults);
        client.hmget.apply(client, args);
    }
    

    EDIT: A version better suited for your example with multi hmget calls.

    // Call with an array of fields and a function(err, results) {}
    function mapResults (fields, next) {
        // Return a closure for a multi.exec call
        return function (err, replies) {
            if(err)
                return next(err);
    
            // Call next with no error and the replies mapped to objects
            next(null, replies.map(mapFields));
        };
    
        function mapFields (reply) {
            var obj = {};
            for(var i = 0, len = fields.length; i < len; i++)
                obj[fields[i]] = reply[i];
            return obj;
        }
    }
    

    Sample usage:

    var client = require("redis").createClient()
      , multi = client.multi();
    
    multi.hmget("a.1", "foo", "bar");
    multi.hmget("a.2", "foo", "bar");
    multi.exec(mapResults(["foo", "bar"], function(err, results) {
        // results will be [{foo: 17, bar: 4711}, {foo: 42, bar: 3.1415926535897932384626433}]
        console.log(results);
    }));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been playing with Node.js for some time. I have the following piece
I have been writing Node.js apps for quite some time now, and I have
I have been using Eclipse as an IDE for a short amount of time
I have been developing Web Applications using WAMP stack. I use javascript jQuery extensively
Sorry if this question is simple but I have been using node.js for only
I'm quite new to programing and have a problem. I have been using touchxml
We have been using CruiseControl for quite a while with NUnit and NAnt. For
I have been using PHP and JavaScript for building my dad's website. He wants
I have been using Castle MonoRail for the last two years, but in a
We have been using Scrum for around 9 months and it has largely been

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.