Reputation: 781
I have a NetSuite SuiteScript file (2.0) in which I want to include a small library of utilities I've built. I can do that fine, and access the functions in the included library. But I can't access the constants I've defined in that library - I have to re-declare them in the main file.
Here's the main file:
define(['N/record', 'N/search', './utils.js'],
function (record, search, utils) {
function pageInit(scriptContext) {
isUserAdmin = isCurrentUserAdmin(contextRecord);
if (isUserAdmin) {
alert('Administrator Role ID is ' + ADMINISTRATOR_ROLE);
// Do something for Admin users
}
return;
}
return {
pageInit: pageInit
};
});
You can see I include the file ./utils.js
in it. Here's utils.js
:
const ADMINISTRATOR_ROLE = 11;
function isCurrentUserAdmin(currentRecord) {
return ADMINISTRATOR_ROLE == nlapiGetRole();
}
That's the entire file - nothing else.
In the main file, the call to the function isCurrentUserAdmin
works fine. It correctly tells me whether the current user is an admin. Note that I don't have to preface the call to isCurrentUserAdmin
with utils.
(utils.isCurrentUserAdmin
doesn't work - it gives me the error JS_EXCEPTION TypeError utils is undefined
). But when the code gets to the line that uses ADMINSTRATOR_ROLE
, I get the error JS_EXCEPTION ReferenceError ADMINISTRATOR_ROLE is not defined
. BTW, if I put the constant definition of ADMINISTRATOR_ROLE
in the main file instead of utils.js
, I get the same error when utils.js
tries to use it. The only way I can get it to work is if I have the line defining the constant in both files.
Why does the inclusion work for the function, but not the constant? Am I including the library wrongly? I thought I'd have to use it as utils.isCurrentUserAdmin
rather than just isCurrentUserAdmin
, but to my surprise that's not the case, as I say above.
Upvotes: 1
Views: 490
Reputation: 1341
You need to make sure any member you need to access in another module needs to be exported in the source module using the return statement
Upvotes: 0
Reputation: 919
If you have utils.js like below, you can use utils.ADMINISTRATOR_ROLE and utils.isCurrentUserAdmin() in your main file.
/**
*@NApiVersion 2.0
*/
define ([],
function() {
const ADMINISTRATOR_ROLE = 11;
function isCurrentUserAdmin() {
// check here
}
return {
ADMINISTRATOR_ROLE: ADMINISTRATOR_ROLE,
isCurrentUserAdmin: isCurrentUserAdmin
};
});
Upvotes: 3