简体   繁体   中英

How to share a global variable between server and client code in Meteor

I'm trying to share a varible between server and client code.I have declared the global variable in /lib/environment.js

test = null;

In the server/main.js,I'm updating this variable when I receive a POST request.

test ="hello";

The problem is if I try to access the same variable inside client/main.js file, the value doesn't get updated to the new value. I'm not understanding this behavior.

Because Meteor works in such an integrated way, it makes you think this is possible, but the server process will run on a server, and the client runs in the browser. Easy to forget that.

To share data, the easiest way is to use a collection, which if published and subscribed, gives you the same effect as a global variable.

Otherwise you can use Meteor.call() to pass data from client to server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM