javascript - 对全局变量及其使用方法的困惑

标签 javascript node.js jsreport

我想我对全局变量在 Node.js 中如何工作有点困惑。我有这个代码:

var jsreport = require('jsreport-core')()
var fs = require('fs');
var path = require('path');
// make sure to pass the path to your `helper.js`
var helpers = fs.readFileSync(path.join('/Development/jsreport-new/data/templates/Sample report', 'helpers.js'), 'utf8');

var data = fs.readFileSync(path.join('/Development/jsreport-new', 'scratch.json').toString(), 'utf8');
var json = JSON.parse(data);
jsreport.init().then(function () {
    return jsreport.render({
        template: {
            scripts: [{
                content: "request.data={endpoints: json }; done();"
            }],
            content: fs.readFileSync(path.join('/Development/jsreport-new/data/templates/Sample report', 'content.handlebars'), 'utf8'),
            helpers: helpers,
            engine: 'handlebars',
            recipe: 'phantom-pdf',
            phantom: {
                "orientation": "portrait",
                "format": "A3",    
                "margin": "3cm",
                "headerHeight": "3cm"
            },
        },
        data: {
            "books": [
                {"name": "A Tale of Two Cities", "author": "Charles Dickens", "sales": 351},
                {"name": "The Lord of the Rings", "author": "J. R. R. Tolkien", "sales": 125},
                {"name": "The Da Vinci Code", "author": "Dan Brown", "sales": 255},
                {"name": "The Hobbit", "author": "J. R. R. Tolkien", "sales": 99},
                {"name": "Carlskii", "author": "J. R. R. Tolkien", "sales": 99}
            ]
        }
    }).then(function(resp) {
        //prints pdf with headline Hello world
        console.log(resp.content.toString())
        resp.result.pipe(fs.createWriteStream('helloworld4.pdf'));
        setTimeout(function() {
            process.exit();
        }, 3000)
    });
}).catch(function(e) {
    console.log(e)
});

我需要将从本地文件读取的 json 数据传递到 jsreport 模板。即需要传递给模板内的内容 content: "request.data={endpoints: json }; done();"

但是,我只是得到[错误:json未定义]

然后我尝试将 json 变量定义为全局变量。例如global.json = JSON.parse(data);,但是现在有所不同。

最佳答案

这里的 json 变量实际上并不是全局的。它位于 Node 模块范围的本地,其他模块无法访问它。

这意味着,当您的报告在其自己的范围内解析并执行 "request.data={endpoints: json }; done();" 时,它并不知道 json.

要回答有关何时使用全局变量的问题,稍微尖刻但有效的答案是“从不”。始终建议管理数据可访问性。相反,我建议您直接将 json 数据包含在上下文值中,如下所示:

scripts: [{
    content: "request.data={endpoints: " + JSON.stringify(json) + " }; done();"
}] 

关于javascript - 对全局变量及其使用方法的困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36519578/

相关文章:

performance - mongoose 在这种情况下子文档和全局之间的性能有什么区别吗?

javascript - Azure Function App jsreport on HTTP 触发器不起作用

javascript - FadeIn//FadeOut 一些引用

javascript - 使用 jQuery、JSON 和 PHP 从目录中获取文件大小和文件日期

javascript - 箭头函数在三元运算符中不起作用

javascript - 在 Nodejs/V8 扩展中通过 Javascript 将 C++ 对象传递给 C++ 函数

node.js - Dynamoose 中 Schema.Types.ObjectId 的等效项

c# - JsReport Recipe ChromePdf 页码

node.js - 尝试从一条 Express 路线获取内容作为 jsreports 中 pdf 报告的内容

javascript - Angular2 路由守卫返回 Observable<bool>,如何处理错误