I am trying to pass a file name into my JavaScript script, similarly to the example code here: https://mupdf.com/docs/examples/draw-document.js
C:\Tools\mupdf-1.14.0-windows> .\mutool.exe run .\script.js c:\temp\test.pdf
Unfortunately, I'm getting the following error:
ReferenceError: 'argv' is not defined
The documentation (https://mupdf.com/docs/manual-mutool-run.html) says:
[...] the command line arguments are accessible from the global 'argv' object.
The underlying code looks like this (this is just 'proof of concept' code):
var doc = new Document(argv[1]);
var outline = doc.loadOutline();
var outlineString = JSON.stringify(outline, {}, 2);
var txt = new ActiveXObject("Scripting.FileSystemObject");
var s = txt.CreateTextFile("c:\\temp\\text.json", true);
s.Write(outlineString);
s.Close();
Could someone point out the error I'm making please?
Just to point out that in a perfect world, I'd like to convert the JSON object to an XML file.
From the MuTool 1.14.0-rc1 Changelog:
mutool run: Pass arguments to script in scriptArgs global.
So, you should use scriptArgs rather than argv in your script.
Cheers,
Rom
Related
I wan to test my library with nodeunit, and I use File Object in it, on website everything is working (FileAPI is implemented there) but when I'm trying to test it with nodeunit i get an error:
Fatal error: File is not defined
I assume that I have to add:
var FileAPI = require('file-api');
var File = FileAPI.File;
at the begging of the code, but I don't need that when I include that library to website, how to deal with that?
To use nodeunit I had to add module.exports at the end, is it necessary as well? (code sample on github)
What's more, when I'm trying to test this code:
https://github.com/GeoSmartCity-CIP/gsc-client/blob/feature/upload-data-file/src/upload/upload.js
with this test:
var gsc = require('../../src/upload/upload');
var FileAPI = require('file-api');
var File = FileAPI.File;
var exports = exports || {};
exports.isFileTypeCorrect = function(test) {
var file = new File('test.geojson');
var asd = new gsc.upload.Data(file);
test.ok(asd.isFileTypeCorrect(), 'this assertion should pass');
test.done();
};
I'm getting Fatal error: Cannot read property 'substr' of undefined error, what's the problem?
EDIT:
problem with substr is propably from isShapefileCorrect function, but still dont know why?
var asd = new gsc.upload.Data(file); Isn't it asynchronous function? Then probably asd.isFileTypeCorrect() should be called inside upload callback. Also you defining isFileTypeCorrect function as module exports and call itself inside itself. Isnt it an infinite loop?
I have a Node program. This program is importing some code. That code looks like the following:
sample.js
function SampleModel() {
this.name = 'Test';
}
module.exports = SampleModel;
I am using SampleModel in another file. Currently, I am successfully using it like this:
another.js
var SampleModel = require('./sampleModel');
var model = new SampleModel();
For the purpose of education, is there a way to condense these two lines down to a single line of JavaScript? If so, how?
Thank you!
Like this:
var model = new (require('./sampleModel'))();
It's quite common to access the required module directly in the import line, but you'll mostly see property access (like require('events').EventEmitter).
I am new to javascript and Node.js and having problems testing some code I wrote recently. I am trying to test code written in a file called "compareCrowe.js" with "testCrowe.js" using Node.js.
Here are the contents of testCrowe.js:
var compareCrowe = required['./compareCrowe'];
console.log('begin test');
var connection = {Type:1, Label:"label", linkTo:null};
var table1 = {name:"table1", body:"description1", out:[connection]};
var table2 = {name:"table2", body:"description2", out:null};
connection.linkTo = table2;
var crowe = [table1, table2];
var result = compareCrowe.compareCrowesFoot(crowe, crowe);
console.log(result.feedback);
where the function "compareCrowesFoot" is defined in compareCrowe.js. From the console on an Ubuntu virtual machine I ran:
node compareCrowe.js testCrowe.js
however, nothing was printed. There were no errors or warnings or explanation of any kind. It didn't even print the "begin test" line I placed at the top of testCrowe.js. If I run the command:
node testCrowe.js
it complains that compareCrowesFoot is undefined. How can I test the contents of compareCrowe.js?
Welcome to the party of JS.
I'm not sure where you're learning from, but a few of the resources that have helped me and many others are superherojs.com, nodeschool.io, the MDN developer docs, Node.js API docs, and Youtube (seriously).
The basic idea of Node.js is that it operates with modules (chunks of reusable code), which is what NPM is made up of. These can then be included in other modules and used anywhere else in your application.
So for a basic example, say you had compareCrowe.js, to make it includable/reusable in another file, you could write something like:
module.exports = function() {
var compareCrowesFoot = function(crowe1, crowe2) { /* compare crows feet and return something here */ }
return { compareCrowesFoot: compareCrowesFoot };
// return an object with a property of whatever you want to access it as , and the value as your function name
// e.g. - you could return { compare: compareCrowesFoot };
}
Then in testCrowe.js you could require compareCrowe like this:
var compareCrowe = require("./compareCrowe");
/* your code here... */
var result = compareCrowe.compareCrowesFoot(crowe1, crowe2);
// if your returned object was { compare: compareCrowesFoot };
// this would be compareCrowe.compare(crowe1, crowe1);
And to run your tests, you could then run node testCrowe.js from the command line.
In your case it seems like you've got your syntax a little messed up. It should be more like:
var compareCrowe = require('./compareCrowe.js');
That would make any methods you've exported in compareCrowe.js, such as your compareCrowe.compareCrowesFoot function, available to testCrowe.js.
Then, in your terminal, you would run the following:
node testCrowe.js
And that should do the trick provided you don't have any further errors in your code.
I'm trying to build an app with meteor.js. In the lib directory, I've got a file collections.js that looks like:
var Datum = new Meteor.Collection('datum');
if (Meteor.isServer && Datum.find().count() == 0) {
var datum = [{...}]
_.each(datum, function(data) {
Datum.insert(data);
});
}
And then in my .js file in the client directory looks like:
Template.datum.helpers({
datum: function() {return Datum.find(); }
});
When I try to run the app, I get Uncaught ReferenceError: Datum is not defined and a blank page. I can't for the life of me figure out why that's happening. Any suggestions or help? I'm sure it must me be doing something stupid but I just can't figure out what it is.
Remove the var keyword.
All javascript files in Meteor are embedded in the (function(){...})() pattern, therefore all variables defined with the var keyword are local to the file. If you want to create a global variable, visible to the whole application, do it without the keyword.
So, instead of
var Datum = new Meteor.Collection('datum'); // local
there should be
Datum = new Meteor.Collection('datum'); // global
I am trying to reuse a JavaScript library (sim.js) in CoffeeScript code. In an example that comes with sim.js, there are 3 files, "sim-0.26.js", "buffet_restaurant.js", and "buffet_restaurant.html" wired together like this (I've eliminated some parameters to simplify):
Inside of "buffet_restaurant.html", there is
<script type="text/javascript" src="./buffet_restaurant_files/sim-0.26.js"></script>
<script type="text/javascript" src="./buffet_restaurant_files/buffet_restaurant.js"></script>
<script type="text/javascript">
$(function () {
$("#run_simulation").click(function () {
var results = buffetRestaurantSimulation();
...
where buffetRestaurantSimulation is a function inside of buffet_restaurant.js. The code in buffet_restaurant.js starts off like this:
function buffetRestaurantSimulation()
{
var sim = new Sim();
...
where Sim is defined in sim-0.26.js, like this:
function Sim(){...};
This example runs fine. I want to reuse "Sim" in a CoffeeScript file in node.js. So I try this (invoked from jasmine-node):
sjs = require "./sim-0.26.js"
mysim = new sjs.Sim()
where sim-0.26.js is in the same directory as testsim.spec.coffee, the file containing this code. When I invoke this using:
jasmine-node --coffee ./testsim.spec.coffee
I get this:
mysim = new sjs.Sim();
^
TypeError: undefined is not a function
I suspect I am doing a ton of things wrong. I'm pretty new to all this.
Any ideas?
Unless Sim is added to exports it will not be available when required - see the docs on modules for more information on the way Node.js modules need to be written.
If this needs to work in both the browser and in node, then simply add this to the code for sim.js:
var root = typeof exports === "object" ? exports : window;
root.Sim = Sim;