Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
node.js i18n:“文件”;ReferenceError:“未定义”;_Node.js_Cron_Internationalization_I18n Node - Fatal编程技术网

node.js i18n:“文件”;ReferenceError:“未定义”;

node.js i18n:“文件”;ReferenceError:“未定义”;,node.js,cron,internationalization,i18n-node,Node.js,Cron,Internationalization,I18n Node,在整个应用程序中,我使用i18n没有问题。但是,对于通过cron作业发送的电子邮件,我会收到以下错误: ReferenceError:\未定义 在app.jsI配置i18n: const i18n = require("i18n"); i18n.configure({ locales: ["en"], register: global, directory: path.join(__dirname, "locales&qu

在整个应用程序中,我使用
i18n
没有问题。但是,对于通过cron作业发送的电子邮件,我会收到以下错误:

ReferenceError:\未定义

app.js
I配置i18n:

const i18n = require("i18n");
i18n.configure({
    locales: ["en"],
    register: global,
    directory: path.join(__dirname, "locales"),
    defaultLocale: "en",
    objectNotation: true,
    updateFiles: false,
});
app.use(i18n.init);
在我的整个应用程序中,我使用它作为
\('authentication.flash.not logging')
,就像我说的那样,没有问题。在由cron作业调用的邮件控制器中,我以相同的方式使用它:
\uuu('mailers.buttons.upgrade now')
。然而,只有在那里,它才会产生上述错误

为了尝试,我在邮件控制器中将其更改为
i18n.\uuu('authentication.flash.not logging')
。但我又犯了一个错误:

(node:11058) UnhandledPromiseRejectionWarning: TypeError: logWarnFn is not a function
    at logWarn (/data/web/my_app/node_modules/i18n/i18n.js:1180:5)

知道如何使通过cron作业发送的电子邮件正常工作吗?

在评论中,询问者澄清cron作业直接调用
mailController.executecons()
,而不是向应用程序发出HTTP请求。因此,
i18n
全局对象从未定义,因为
app.js
中的应用程序设置代码未运行

最好的解决方案是使用
i18n
。您可以将
I18N
对象的实例化和配置分离到一个单独的函数中,然后在
app.js
中调用它以将其设置为Express中间件,并在
mailController.executeCrons()
函数中调用它以在通过cronjob调用时使用它

代码大纲:

i18n.js
(新文件)

app.js

const configureI18n = require('./path/to/i18n.js');

const [i18n, _] = configureI18n(true);
app.use(i18n.init);
const configureI18n = require('./path/to/i18n.js');

const [_, i18nObj] = configureI18n(false);

executeCrons() {
  i18nObj.__('authentication.flashes.not-logged-in');
}
mailController.js

const configureI18n = require('./path/to/i18n.js');

const [i18n, _] = configureI18n(true);
app.use(i18n.init);
const configureI18n = require('./path/to/i18n.js');

const [_, i18nObj] = configureI18n(false);

executeCrons() {
  i18nObj.__('authentication.flashes.not-logged-in');
}

你的cron工作是如何设置的?它是向你的应用程序发出HTTP请求,还是尝试使用
node
直接运行控制器?@DylanSp,我对cron了解不多,自己也没有设置过,所以我希望我回答的问题是正确的。如果我运行
crontab-l
我有
3014***cd/data/web/portal/&&/usr/bin/timeout 120/usr/bin/node cli.js sendmails 2>&1 |/usr/bin/logger-t portal cron live
。我认为这意味着node试图直接运行控制器,不是吗?这取决于
cli.js sendmails
的功能。该脚本是发出HTTP请求还是直接调用控制器?啊,直接调用控制器:
program.command(“sendmails”).action(异步函数(){mailController.executecons())…
这是您的问题;
i18n
global没有这样定义。我将键入一个答案。我现在正在尝试解决方案,但引用app.js会产生错误:
const[i18n,]=configureI18n(true)
类型错误:configureI18n不是函数或其返回值不可编辑
。很抱歉,将导出语法弄乱了。编辑
i18n.js
以使用
exports=configureI18n;
,尝试一下。太好了,它可以工作了!谢谢!不客气,很高兴我能提供帮助。