0

I have successfully connected to MongoDb and used a form to send some data. I would now like to store the data in a json document with a collection. So my current output is:

{
    "_id": "53be957007d2c838083046a6",
    "subscriberinfo": "X",
    "grouporpolicynumber": "X",
    "title": "X",
    "clientnumber": "X",
    "xnumber": "X",
    "postedOn": "2014-07-10T13:30:24.499Z"
}

I would like it to look like:

{
    "_id": "53be957007d2c838083046a6",
    "ReferenceInfo": {
          "subscriberinfo": "00003",
          "grouporpolicynumber": "Direct",
          "title": "SNP",
          "clientnumber": "VG00003M",
          "HICnumber": "264134187C"
                     }
    "postedOn": "2014-07-10T13:30:24.499Z"
}

Current code looks like this:

   function postNewJob(req , res , next){
        var job = {};


    job.subscriberinfo              = req.params.subscriberinfo;
    job.grouporpolicynumber         = req.params.grouporpolicynumber;
    job.title                       = req.params.clientreportingcategory;
    job.clientnumber                = req.params.clientnumber;
    job.HICnumber                   = req.params.hicnumber;
    job.postedOn                    = new Date();

    res.setHeader('Access-Control-Allow-Origin','*');

    memberInfo.save(job , function(err , success){
        console.log('Response success '+success);
        console.log('Response error '+err);
        if(success){
            res.send(201 , job);
            return next();
        }else{
            return next(err);
        }
    });
}
user3821263
  • 121
  • 1
  • 8

1 Answers1

0

Have you tried something like this?

function postNewJob(req , res , next){
    var job = {
        referenceInfo: {
            subscriberinfo      : req.params.subscriberinfo,
            grouporpolicynumber : req.params.grouporpolicynumber,
            title               : req.params.clientreportingcategory,
            clientnumber        : req.params.clientnumber,
            HICnumber           : req.params.hicnumber
        },
        postedOn: new Date();
    };

    res.setHeader('Access-Control-Allow-Origin','*');

    memberInfo.save(job, function(err, job){
        if(err) {
            console.error('Response error ' + err);
            next(err);
        } else {
            console.log('Response success ' + job);
            res.send(201, job);
            next(req, res);
        }
    });
}

On another note, I changed around your callback function that's passed in to save. Mongo will call the function, passing in error and data (data in this case being job) which allows you to conditionally log error or success depending on the status of the save. Also, because those are asynchronous functions, returning the call of next() will not actually impact the code. Also, when calling next(), it is advisable to pass along the req and res from before, so that more actions can be taken from that data.

Hope that helps!

Mercury
  • 350
  • 2
  • 7
  • It works great!! Thanks for your help!. On a side note do you prefer mongojs or Mongoose to interact with Mongo DB? – user3821263 Jul 11 '14 at 12:51
  • Awesome! Glad it works for you! I usually use mongoose-- it gives some extra functionality that makes querying easier, but it also requires you to specify a schema for your database. Overall, I think that mongoose does make mongo a bit easier to interact with, though. – Mercury Jul 11 '14 at 22:44