使用 Luxon.js 在 NetSuite 服务器端脚本中

huangapple go评论42阅读模式
英文:

Using Luxon.js within NetSuite Server Side Scripts

问题

我已经成功在NetSuite客户端脚本中多次使用Luxon。

是否可以在服务器端脚本中使用它?

我已尝试使用与客户端脚本相同的命名约定:

define(['N/https', './luxon'], (https) => {

        const beforeLoad(ctx) {
            let DateTime = luxon.DateTime;
            let Duration = luxon.Duration;

            let today = DateTime.now()

        }

    etc....
}

库可以被正常找到,但每当我声明 let today = DateTime.now() 时,总是收到以下错误信息:

"error": {
    "name": "ReferenceError",
    "message": "Intl is not defined",
    "stack": "ReferenceError: Intl is not defined
at systemLocale (/SuiteScripts/luxon.js:963:28)
at Function.create (/SuiteScripts/luxon.js:...:65)
at new DateTime (/SuiteScripts/luxon.js:...:39)
at Function.now (/SuiteScripts/luxon.js:...:14)
at Object.onRequest (/SuiteScripts/suitelet.js:...:39)"
  }

我认为这是因为它未在浏览器中调用。

我希望它能返回服务器时区的对象。

英文:

I've successfully used Luxon many times within NetSuite client scripts.

Is there a way to use it within server-side scripts?

I've tried with the same naming convention as client script

define(['N/https', './luxon'], (https) => {

        const beforeLoad(ctx) {
            let DateTime = luxon.DateTime;
            let Duration = luxon.Duration;

            let today = DateTime.now()

        }

    etc....
}

The library is found with no issue, but always get the following when I declare let today = DateTime.now()

"error": {
    "name": "ReferenceError",
    "message": "Intl is not defined",
    "stack": "ReferenceError: Intl is not defined
at systemLocale (/SuiteScripts/luxon.js:963:28)
at Function.create (/SuiteScripts/luxon.js:...:65)
at new DateTime (/SuiteScripts/luxon.js:...:39)
at Function.now (/SuiteScripts/luxon.js:...:14)
at Object.onRequest (/SuiteScripts/suitelet.js:...:39)"
  }

I'm assuming this is because it's not called within a browser.

I was hoping it would return the object in the timezone of the server.

答案1

得分: 1

以下是翻译好的部分:

"在我发布上面的帖子后,我搜索了一下,找到了一个答案:

globalThis.Intl = {
DateTimeFormat: function() {
this.resolvedOptions = function(){
return {
locale: "en-US"
}
}
}
}

onRequest函数的开头声明了这个,现在它可以工作了。"

英文:

And a couple of googles after I made the post above, I came across an answer:

globalThis.Intl = {
    DateTimeFormat: function() {
        this.resolvedOptions = function(){
            return {
                locale: "en-US"
            }
        }
    }
}

Declared this at the start of the onRequest function, and now it works.

huangapple
  • 本文由 发表于 2023年6月16日 07:02:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76486000.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定