1

am using mapfunction in my code i would like to pring Qdata of below array but here it says undefined may be it is inside the array of json if am wrong please help me to correct this! my array structure is like this

    [ { _id: 201,
        user_id: 141,
        PAN: 1,
        Qdata: 023444 } ]

here is the map fun:

    var data = array.map(function(p){
    return p.Qdata
    })

My actual code:

function getTotal(req, res, next) {
    var array = [];
    var array2 = {};
    db.users.find(req.q).toArray(function(err, rest) {
        rest.map(function(ids) {
            array.push(ids._id);
        })
        var total = array.length;
        for (var i = 0; i < array.length; i++) {
            db.users.find(array[i]).toArray(function(err, users) {
                if (err) return next(err);
            });
            db.consumer.aggregate([{
                $unwind: "$votes"
            }, {
                $match: {
                    'consumer_id': mongoskin.helper.toObjectID(array[i])
                }
            }, {
                $group: {
                    _id: "$votes.date",
                    "consumer_id": {
                        $first: "$consumer_id"
                    },
                    "vote": {
                        $sum: {
                            $cond: [{
                                '$gt': ['$votes.score', 0]
                            }, "$votes.score", 0]
                        }
                    }
                }
            }], function(err, sort_res) {
                array2.push(sort_res);
                total -= 1;
                if (total <= 0) {

                var user_obj = {};
                for (var i = rest.length - 1; i >= 0; i--) {
                var userDetails = rest[i];
               user_obj[userDetails._id] = userDetails;
            }

             var aa=array2.map(function(p){
                p.userDetails = obj[p.consumer_id];--------> here am not getting consumer_id
                return p
            })
                res.send(aa)
                }

            })
        }


    })
}
7
  • You're missing a comma after the _id property, and an end paranthesis after the last } in your second code. Commented May 18, 2017 at 11:33
  • @DragoşPaulMarinescu sorry i was my mistake while writing here but its correct in my code. Commented May 18, 2017 at 11:39
  • What doesn't work? I just tried your code in the console and it returns what it should (023444). Commented May 18, 2017 at 11:42
  • i dont know it says undefine for me :( Commented May 18, 2017 at 11:46
  • Can you post your entire code? Commented May 18, 2017 at 11:52

1 Answer 1

4

Just would like to raise few points that might help you.

So for this I would like to recommend using new version of javascript ES6 it have lot of simplified syntax changes and new updates

The output will work if you will use any number not starting from 0 in this case you will get desired output

  var x =[ { _id: 201,
        user_id: 141,
        PAN: 1,
        Qdata: 23444 } ]

      var data  = x.map(v => v.Qdata)
    console.log(data) // 23444

But if you will use any number starting form 0 like the one you have posted in you question '023444' it's treated as base 8 (octal) and would give you output as 10020 because an integer starting with 0 doesn't make sense in mathematics.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.