Gobliins
Gobliins

Reputation: 4026

meteor import global variable how?

Hello i am using Meteor 1.3.5

I have a package in:

projectdir/packages/mypackage

In this package i defined a global variable:

myGlobalVariable = "somedata";

Now i have a myapi.js in:

projectdir/imports/api/myservice

I want to use myGlobalVariable in myapi.js. So i included this line:

import {myGlobalVariable} from 'meteor/mypackage';

In my code the global variable is still undefined when i enter the code in myapi.js.

What am i doing wrong?

Upvotes: 1

Views: 726

Answers (2)

Yosvel Quintero
Yosvel Quintero

Reputation: 19060

It is a better practice to have declared variables in your modules and export/import them:

File: projectdir/packages/mypackage:

export let myGlobalVariable = "somedata";

// You can also export multiple variables in one sentence:
export let name1, name2, …, nameN; // Also can use var

If your prefer to have myGlobalVariable as global you can do:

myGlobalVariable = "somedata";

export myGlobalVariable;

File: projectdir/imports/api/myservice:

import {myGlobalVariable} from 'meteor/mypackage';

// You can also import multiple variables in one sentence:
import {name1, name2, …, nameN} from 'meteor/mypackage';

Upvotes: 0

Ashish Patel
Ashish Patel

Reputation: 941

You can kept the global variable inside the lib/constant.js from here you will be able to access it any where

Any files placed in a folder lib/ anywhere in your app structure will be loaded before every other file

lib/constant.js

myGlobalVariable = "somedata"

Upvotes: 1

Related Questions