sessionmeteorsession-variablesmeteorite

how to send regex to server side in meteor


In my application, I'm building a query object some thing like below

Object {pointType: /analog/i, _id: Object}

I tried to store it in session variable,

Session.set("currentPointsQueryObject",queryObj);

Then on click event I'm getting this object

  var res= Session.get("currentPointsQueryObject");
  console.log(res);

but here I'm getting like below

Object {pointType: Object, _id: Object}

Meanwhile, I sent group_id to the server by geting it from session variable like

var group_id=Session.get("currentGroupId");

which is working fine(it is displaying id in server log)

Then, I've tried storing it in global variable, which returning as expected

like below on click event

Object {pointType: /analog/i, _id: Object}

but when I sent it to server side method (Immediate line after console.log() )

Meteor.call("updateGroupPoints",res,function(err,data){
        console.log("updated points");
        console.log(data);
    });

when I log res in server console, it is showing

{ pointType: {}, _id: { '$nin': [] } }

Althoug I have something in pointType, It is not passed to the server.

Anyone had idea, Is this the thing related storing?


Solution

  • You cannot directly serialize RegExp to EJSON, but you can:

    var regexp = /^[0-9]+$/;
    var serialized = regexp.source;
    

    Send serialized and then deserialize:

    new RegExp(serialized)
    

    Take a look at : Meteor: Save RegExp Object to Session