"ReferenceError: <functionName> is not defined" : libraries issue?
"ReferenceError: <functionName> is not defined" : libraries issue?
我对 node.js 非常非常陌生,我可能会问一些非常简单的问题。
客户端:我已经做好了maps on client side via d3js, based on a custom made ./js/wikiatlas.js
(here).
服务器端:我现在想移动这个地图生成服务器端。我使用 node、jsdom 和 d3js 成功输出了一个基本的 square.svg。我现在想使用 ./js/wikiatlas.js
中的更复杂的函数。所以我天真地添加了 ./js/wikiatlas.js
依赖项和 js 函数调用 locationMap("body",800, target, title, WEST, NORTH, EAST, SOUTH);
期望它工作(其他事情肯定有效)。我的完整代码:
var jsdom = require('jsdom');
var fs = require('fs');
jsdom.env(
"<html><body></body></html>", // CREATE DOM HOOK:
[ '../js/jquery-2.1.3.min.js',
'../js/d3.v3.min.js',
'../js/topojson.v1.min.js',
'../js/wikiatlas.js', // <<======== my code (IMPORTANT !!)
'../js/b64.js'], // ... & offline
//'http://rugger-demast.codio.io/js/wikiatlas.js',
function (err, window) {
/* COLLECT ENV.VARIABLES ******************************************* */
var WEST = process.env.WEST,
NORTH = process.env.NORTH,
EAST = process.env.EAST,
SOUTH = process.env.SOUTH,
target= process.env.ITEM,
title = process.env.ITEM,
WIDTH = process.env.WIDTH;
/* D3js FUNCTION *************************************************** */
locationMap("body",800, target, title, WEST, NORTH, EAST, SOUTH); // <<======== defined in wikiatlas.js!
/* SVG PRINT ******************************************************* */
var svgheader = '<?xml version="1.0" encoding="utf-8"?>'
+'<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">';
fs.writeFileSync('administrative_map_(wikiatlas_2014).svg', svgheader + window.d3.select("body").html());
// console.log(window.d3.select("body").html());
}
// END (D3JS) * * * * * * * * * * * * * * * * * * * * * * * *
);
但我随后得到 ReferenceError: locationMap is not defined
错误:
/data/yug/projects_active/make-modules/09_d3/location.node.js:30
locationMap("body",800, target, title, WEST, NORTH, EAST, SOUTH);
^
ReferenceError: locationMap is not defined
at Object.done (/data/yug/projects_active/make-modules/09_d3/location.node.js:30:1)
at /data/yug/projects_active/make-modules/node_modules/jsdom/lib/jsdom.js:270:18
at process._tickCallback (node.js:419:13)
make: *** [output] Error 8
我应该如何加载或构建 ./js/wikiatlas.js
以便我可以在我的 node.js / jsdom 服务器脚本上使用其中的函数?
似乎与以下相关:In Node.js, how do I "include" functions from my other files? ,
由于您将脚本加载到 window 的上下文中,因此您还必须使用 window 访问它,即 window.locationMap
.
jsdom.env(
"<html><body></body></html>", // CREATE DOM HOOK:
[ '../js/jquery-2.1.3.min.js',
'../js/d3.v3.min.js',
'../js/topojson.v1.min.js',
'../js/wikiatlas.js', // <<======== my code (IMPORTANT !!)
'../js/b64.js'],
然后
window.locationMap("body",800, target, title, WEST, NORTH, EAST, SOUTH);
就是这样。
我对 node.js 非常非常陌生,我可能会问一些非常简单的问题。
客户端:我已经做好了maps on client side via d3js, based on a custom made ./js/wikiatlas.js
(here).
服务器端:我现在想移动这个地图生成服务器端。我使用 node、jsdom 和 d3js 成功输出了一个基本的 square.svg。我现在想使用 ./js/wikiatlas.js
中的更复杂的函数。所以我天真地添加了 ./js/wikiatlas.js
依赖项和 js 函数调用 locationMap("body",800, target, title, WEST, NORTH, EAST, SOUTH);
期望它工作(其他事情肯定有效)。我的完整代码:
var jsdom = require('jsdom');
var fs = require('fs');
jsdom.env(
"<html><body></body></html>", // CREATE DOM HOOK:
[ '../js/jquery-2.1.3.min.js',
'../js/d3.v3.min.js',
'../js/topojson.v1.min.js',
'../js/wikiatlas.js', // <<======== my code (IMPORTANT !!)
'../js/b64.js'], // ... & offline
//'http://rugger-demast.codio.io/js/wikiatlas.js',
function (err, window) {
/* COLLECT ENV.VARIABLES ******************************************* */
var WEST = process.env.WEST,
NORTH = process.env.NORTH,
EAST = process.env.EAST,
SOUTH = process.env.SOUTH,
target= process.env.ITEM,
title = process.env.ITEM,
WIDTH = process.env.WIDTH;
/* D3js FUNCTION *************************************************** */
locationMap("body",800, target, title, WEST, NORTH, EAST, SOUTH); // <<======== defined in wikiatlas.js!
/* SVG PRINT ******************************************************* */
var svgheader = '<?xml version="1.0" encoding="utf-8"?>'
+'<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">';
fs.writeFileSync('administrative_map_(wikiatlas_2014).svg', svgheader + window.d3.select("body").html());
// console.log(window.d3.select("body").html());
}
// END (D3JS) * * * * * * * * * * * * * * * * * * * * * * * *
);
但我随后得到 ReferenceError: locationMap is not defined
错误:
/data/yug/projects_active/make-modules/09_d3/location.node.js:30
locationMap("body",800, target, title, WEST, NORTH, EAST, SOUTH);
^
ReferenceError: locationMap is not defined
at Object.done (/data/yug/projects_active/make-modules/09_d3/location.node.js:30:1)
at /data/yug/projects_active/make-modules/node_modules/jsdom/lib/jsdom.js:270:18
at process._tickCallback (node.js:419:13)
make: *** [output] Error 8
我应该如何加载或构建 ./js/wikiatlas.js
以便我可以在我的 node.js / jsdom 服务器脚本上使用其中的函数?
似乎与以下相关:In Node.js, how do I "include" functions from my other files? ,
由于您将脚本加载到 window 的上下文中,因此您还必须使用 window 访问它,即 window.locationMap
.
jsdom.env(
"<html><body></body></html>", // CREATE DOM HOOK:
[ '../js/jquery-2.1.3.min.js',
'../js/d3.v3.min.js',
'../js/topojson.v1.min.js',
'../js/wikiatlas.js', // <<======== my code (IMPORTANT !!)
'../js/b64.js'],
然后
window.locationMap("body",800, target, title, WEST, NORTH, EAST, SOUTH);
就是这样。