关于 javascript:为什么 NodeJS 中的这个导出函数不能按我的意愿工作? | 珊瑚贝

Why is this exported function in NodeJS not working as I want it to?


我试图让下面的代码作为中间件从导出的函数中运行。

1
2
3
fs.stat(“./tmp/my_data.json”, (err, stats) => {
scraperDate = stats.mtime.toUTCString();
});

我有

1
let scraperDate =“”;

在我的路线文件顶部。我正在尝试这样做:

1
2
3
router.get(“/”, myFunctions.scraperDateFunction, function (req, res) {
res.render(“my_view”, {scraped: scraped, scraperDate: scraperDate});
});

当我在路线上方按原样运行 scraperDate 代码时,它可以工作。当我将它放在我的functions.js 文件到module.exports 中时,它不会写入scraperDate 变量。

我可能在这里遗漏了一些明显的东西,但我已经尝试让它在两天的大部分时间里工作。这是我的 module.exports 函数:

1
2
3
4
5
6
7
module.exports = {
    scraperDateFunction: function(){
        fs.stat(“./tmp/my_data.json”, (err, stats) => {
            scraperDate = stats.mtime.toUTCString();
        });
    }
}

* 编辑 *

我现在已经试过了

1
2
3
4
5
6
7
8
getScrapeDate: function(req, res, next){
    fs.stat(“./tmp/my_data.json”, (err, stats) => {
        scraperDate = stats.mtime.toUTCString();
        console.log(err)
        console.log(stats)  
        return next;        
    });
}

按预期打印 stats 以在没有任何错误的情况下进行控制台。这可能与范围有关。我如何将 stats.mtime.toUTCString(); 的结果传递给路由中的 scraperDate 变量?

* 编辑 2 *

现在我的函数文件中有这个

1
2
3
4
5
6
7
8
9
10
    getScrapeDate: function(req, res, next){
    fs.stat(“./tmp/my_data.json”, (err, stats) => {
        if (err) {
            next (err);
        } else {
            res.locals.scraperDate = stats.mtime.toUTCString()
        }

    });
}

按照建议在我的路线文件中,但它不会加载我的视图

1
2
3
4
5
router.get(“/”, myFunctions.getScrapeDate, function (req, res) {
    let {scraperDate} = res.locals;
    res.render(“my_view”, {scraped: scraped, scraperDate:
    scraperDate});
});

scraped 在路由文件的顶部声明。

* 最终编辑 *

这是一个正在运行的设置

1
2
3
4
router.get(“/”, myFunctions.getScrapeDate, function (req, res) {
    let {scraperDate} = res.locals;
    res.render(“my_view”, {scraped, scraperDate});
 });

1
2
3
4
5
6
7
8
9
10
    getScrapeDate: function(req, res, next){
    fs.stat(“./tmp/my_data.json”, (err, stats) => {
        if (err) {
            next (err);
        } else {
            res.locals.scraperDate = stats.mtime.toUTCString();
            next();
        }                  
    });
}
  • 您可以在 fs.stat 回调中添加 console.log(err) 吗?
  • 中间件需要接受一个 next 回调并调用它——尤其是如果它是异步的
  • 范围在模块之间不共享,一个中定义的变量在另一个中不可见



不要在你的函数模块顶部定义 scraperDate,也不要试图将它导入你的路由模块。充其量这是代码异味,而在最坏的情况下,您最终会在不应该共享的单独请求-响应周期之间无意中泄漏信息。

您应该按照此建议使用 res.locals:

在中间件之间传递数据

在你的路由文件中:

1
2
3
4
5
6
router.get(‘/’, myFunctions.scraperDateFunction, (req, res) => {
  const { scraperDate } = res.locals;
  // the following is shorthand in ES2015 syntax for
  // { scraped: scraped, scraperDate: scraperDate }
  res.render(‘my_view’, { scraped, scraperDate });
});

在你的函数文件中:

1
2
3
4
5
6
7
8
9
10
11
12
module.exports = {
  scraperDateFunction (req, res, next) {
    fs.stat(‘./tmp/my_data.json’, (err, stats) => {
      if (err) {
        next(err);
      } else {
        res.locals.scraperDate = stats.mtime.toUTCString();
        next();
      }
    });
  }
}

同样,不要尝试在函数文件中定义和导出顶级 scraperDate 变量,这是在 express 中的中间件之间传递数据的一种更简洁的方法。

  • @75martin 在 else 中对 next() 的调用不是可选的。重新阅读我的答案。


模块中 scraperDateFunction 中的 scraperDate 变量不会被导出,因为不同模块之间不会共享作用域。这会破坏模块的全部目的。

查看参考文档。

  • 是的,帕特里克埃文斯提到了这一点。如何访问函数的统计信息部分?
  • fs.stat(“./tmp/my_data.json”, (err, stats) => { return stats.mtime.toUTCString(); });
  • 试过 fs.stat(“./tmp/my_data.json”, (err, stats) => { return stats.mtime.toUTCString()});但它似乎没有通过日期……
  • @PatrickRoberts 我现在正在涉足它……我记得在其他地方使用 res.locals,谢谢你的建议!病态更新
  • @75martin 这就是 res.locals 对象的用途。不支持或记录直接在请求对象上设置变量。 res.locals 保证在请求的生命周期内保持状态。
  • @JavierSilvaOrtíz 是的,我不知道为什么有人对他的第一个回复投了反对票……:D


来源:https://www.codenong.com/59060231/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_8874.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?