Meteor包:Session.set根本不起作用

时间:2021-12-14 15:53:57

I have inside a Meteor package, a global function in a js file I add via api.addFiles:

我有一个Meteor包,一个js文件中的全局函数,我通过api.addFiles添加:

showLayer = function(id) {
    var layers = Session.get('layers');
    layers[id] = true;
    Session.set('layers', layers);

    // just for testing - check if it worked
    var tmpLayers = Session.get('layers');
    alert('layers.id:' + layers[id] + ' tmpLayers.id:' + tmpLayers[id]);
    window.setTimeout(function() {
        var tmpLayers = Session.get('layers');
        alert('layers.id:' + layers[id] + ' tmpLayers.id:' + tmpLayers[id]);
    }, 1000);
};

Note that the setTimeout I am only using to give Session some time if there should be some optimisation delaying the change. The result is that layers[id] is set as it should but tmpLayers[id] is not, tested delayed and right after set.

请注意,如果应该有一些优化延迟更改,setTimeout我只用于给Session一些时间。结果是,layer [id]被设置为应该但是tmpLayers [id]没有,被测试延迟并且在设置之后。

No matter if I check delayed via setTimeout or right in place inside the function: the Session.set line does not seem to do any thing whatsoever!

无论我是通过setTimeout检查延迟还是在函数内部正确检查:Session.set行似乎没有做任何事情!

Note that this is not a problem of not occurring reactivity - or at least I would not see how.

请注意,这不是没有发生反应的问题 - 或者至少我不会看到如何。

1 个解决方案

#1


Short solution: use an { } instead of an [ ].

简短解决方案:使用{}而不是[]。

I found a possible reason through this stack overflow question: Javascript arrays and Meteor session.

我通过这个堆栈溢出问题找到了一个可能的原因:Javascript数组和Meteor会话。

However: the solution provided there did not solve the problem for me. What did solve it was to make the [ ] an { }. No cloning needed, it worked out of the box. This issue I think might be a bug, but I could not easily reproduce it.

但是:那里提供的解决方案并没有为我解决问题。解决它的是让[]成为{}。不需要克隆,它开箱即用。我认为这个问题可能是一个错误,但我无法轻易重现它。

#1


Short solution: use an { } instead of an [ ].

简短解决方案:使用{}而不是[]。

I found a possible reason through this stack overflow question: Javascript arrays and Meteor session.

我通过这个堆栈溢出问题找到了一个可能的原因:Javascript数组和Meteor会话。

However: the solution provided there did not solve the problem for me. What did solve it was to make the [ ] an { }. No cloning needed, it worked out of the box. This issue I think might be a bug, but I could not easily reproduce it.

但是:那里提供的解决方案并没有为我解决问题。解决它的是让[]成为{}。不需要克隆,它开箱即用。我认为这个问题可能是一个错误,但我无法轻易重现它。