user1934044
user1934044

Reputation: 516

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?

Upvotes: 2

Views: 498

Answers (4)

avalanche1
avalanche1

Reputation: 3592

Simply stringify your RegExp via .toString(), send it to the server and then parse it back to RegExp.

Upvotes: 0

4Z4T4R
4Z4T4R

Reputation: 2458

There's a handy way to teach EJSON how to serialize/parse Regular Expressions (RegExp) as of 2015 documented in this SO question:

How to extend EJSON to serialize RegEx for Meteor Client-Server interactions?

Basically, we can extend the RegExp object class and use EJSON.addType to teach the serialization to both client and server. Hope this helps someone out there in the Universe. :)

Upvotes: 0

Kuba Wyrobek
Kuba Wyrobek

Reputation: 5273

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

Upvotes: 3

Peppe L-G
Peppe L-G

Reputation: 8345

/analog/i is a regular expression, right? Values stored in Session and values sent to methods must be part of EJSON values. Regular expression aren't.

Upvotes: 1

Related Questions