流星:将RegExp对象保存到会话 [英] Meteor: Save RegExp Object to Session

查看:82
本文介绍了流星:将RegExp对象保存到会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在MongoDB查询的流星会话中将正则表达式另存为RegExp对象,但是在Session.get()之后,RegExp对象只是一个空对象.

I'm trying to save a regular expression as RegExp Object in a Meteor Session for a MongoDB query, but after Session.get() the RegExp object is just a empty object.

js

if (Meteor.isClient) {
    Meteor.startup(function () {
          var obj = {};
          obj['regexp'] = new RegExp("test");
          console.log("setting regular expression: " + obj['regexp']);
          Session.set("test", obj);
    });

    Template.test.events({
        'click button': function () {
            var regex = Session.get("test");
            console.log("now it is: ");
            console.log(regex['regexp']);
        }
    });
}

if (Meteor.isServer) {
}

html

<head>
  <title>meteor-regexp-session-test</title>
</head>

<body>
  {{> test}}
</body>

<template name="test">
  <button>hit the button and look at the console</button>
</template>

有什么想法为什么不起作用?

Any ideas why this is not working?

提前谢谢!

推荐答案

Session软件包在内部使用了ReactiveDict.

Session package uses ReactiveDict under the hood.

ReactiveDict序列化传递给Sessions.set(key, value)的值:

// https://github.com/meteor/meteor/blob/devel/packages/reactive-dict/reactive-dict.js
// line 3-7:


var stringify = function (value) {
  if (value === undefined)
    return 'undefined';
  return EJSON.stringify(value);
};

Session.get(key)EJSON.parse反序列化:

// https://github.com/meteor/meteor/blob/devel/packages/reactive-dict/reactive-dict.js
// line 8-12:

var parse = function (serialized) {
  if (serialized === undefined || serialized === 'undefined')
    return undefined;
  return EJSON.parse(serialized);
};

这意味着Session不支持RegExp.

您的问题的解决方案是创建自定义反应式数据源,该源将与Session相似,但不会序列化/反序列化值对象.

The solution for your issue is to create custom reactive data source, which will work similar to Session but will not serialize/deserialize value object.

在这里看看:

  • http://blog.benmcmahen.com/post/48367809759/meteors-reactive-data-sources
  • https://www.eventedmind.com/feed/meteor-build-a-reactive-data-source

这篇关于流星:将RegExp对象保存到会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆