Uncaught TypeError: Object ClipperJS - javascript

I am getting the following error when trying to run my Javascript file:
"Uncaught TypeError: Object function (a){"undefined"==typeof a&& a=0);this.m_PolyOuts=null;this.m_ClipType= d.ClipType.ctIntersection;this.m_IntersectNodeComparer=this.m_IntersectList=this.m_SortedEdges=this.m_ActiveEdges=this.m_Scanbeam=null;this.m...<omitted>...y'"
Relevant code:
<script>
// Geolocation
var stateData;
var states = new Object();
d3.json("states.json", function (data) {
stateData = data;
data.features.forEach(function (datum) {
// Populate data for each entry in states[__]...
});
var pt = new ClipperLib.IntPoint(67.007915, -152.002047);
for (var i = 0; i < states["Alaska"].length; ++i) {
var done = 0;
for (var j = 0; j < states["Alaska"][i].length; ++j) {
var poly = states["Alaska"][i][j];
if (ClipperLib.Clipper.PointInPoly(pt, poly) == 1) {
done = 1;
break;
}
...
}
}
...
});
</script>
When I try running the command on the console in Chrome, I do not receive an error:
https://www.dropbox.com/s/cm29oaxgr5rjz2d/Screenshot%202014-02-28%2011.42.19.png
I am using ClipperJS and have at the top of my file included it as a src as such:
< script src="clipper.js" charset="utf-8">< /script>
(No spaces before "script" or "/script" - they are here because it would not display otherwise.)
I thought it might have been because of a type mismatch but I then tried:
...
var poly = [{X:10,Y:10},{X:110,Y:10},{X:110,Y:110},{X:10,Y:110}];
if (ClipperLib.Clipper.PointInPoly(pt, poly) == 1) {
done = 1;
break;
}
...
And I still got the same error.
Any help is appreciated!
Relevant link for where I got the idea to use ClipperJS (answer by "Timo")

Without seeing the rest of your code, it seems like a bootstrapping issue. Try ensuring that your code is run after everything on the page has run already.
Also: D3's json request calls its callback with error as the first parameter, and data as the second. I suspect this is causing the Poly configurations for each state to be misconfigured or, not configured at all, so when you're querying them for point inclusion it's failing.
https://github.com/mbostock/d3/wiki/Requests

Related

After Effects Script works manually, but not on start up

I have a script (.jsx) that runs correctly when I load the project and run it myself. However, when I run it on start up by putting it in the StartUp folder it gives me this error:
"Unable to execute script at line 1. After Effects error: Unable to call "item" because of parameter 1. The range has no values"
Here is my script:
var compToBeOutput = app.project.item(35);
var comp1 = app.project.item(1);
var numGames = 7;
var p = 3;
compToBeOutput.duration = (90*numGames)/30;
compToBeOutput.layers.add(comp1);
for (i = 0; i<numGames-1; i++) {
var newComp = comp1.duplicate();
var newLayer = compToBeOutput.layers.add(newComp);
newLayer.startTime = p;
p = p + 3;
}
Thanks,
Greg
That is because item here is undefined. app.project.item is only accessible when project is open. so you need to open project first in the script itself.
For example.
var myPath = new File("you file path here");
if(myPath.exists){
app.open(myPath );
}
then paste your script underneath.

PDFJS stopAtErrors does not stop execution when encountering PDF parsing errors

I am using PDFJS to get textual data from PDF files, but occasionally encountering the following error:
Error: Invalid XRef table: unexpected first object.
I would prefer that my code just skip over problem files and continue on to the next file in the list. According to PDFJS documentation, setting stopAtErrors to true for the DocumentInitParameters in PDFJS should result in rejection of getTextContent when the associated PDF data cannot be successfully parsed. I am not finding such to be the case: even after setting stopAtErrors to true, I continue to get the above error and the code seems to be "spinning" on the problem file rather than just moving on to the next in the list. It is possible that I haven't properly set stopAtErrors to true as I think I have. A snippet of my code is below to illustrate what I think I've done (code based on this example):
// set up the variables to pass to getDocument, including the pdf file's url:
var obj = {};
obj.url = http://www.whatever.com/thefile.pdf; // the specific url linked to desired pdf file goes here
obj.stopAtErrors = true;
// now have PDF JS read in the file:
PDFJS.getDocument(obj).then(function(pdf) {
var pdfDocument = pdf;
var pagesPromises = [];
for (var i = 0; i < pdf.pdfInfo.numPages; i++) {
(function (pageNumber) {
pagesPromises.push(getPageText(pageNumber, pdfDocument));
}) (i+1);
}
Promise.all(pagesPromises).then(function(pagesText) {
// display text of all the pages in the console
console.log(pagesText);
});
}, function (reason) {
console.log('Error! '+reason);
});
function getPageText(pageNum, PDFDocumentInstance) {
return new Promise(function (resolve, reject) {
PDFDocumentInstance.getPage(pageNum).then(function(pdfPage) {
pdfPage.getTextContent().then(function(textContent) { // should stopAtErrors somehow be passed here to getTextContent instead of to getDocument??
var textItems = textContent.items;
var finalString = '';
for (var i = 0; i < textItems.length; i++) {
var item = textItems[i];
finalString += item.str + " ";
}
resolve(finalString);
});
});
}).catch(function(err) {
console.log('Error! '+err);
});
}
One thing I am wondering is if the stopAtErrors parameter should somehow instead be passed to getTextContent? I have not found any examples illustrating the use of stopAtErrors and the PDFJS documentation does not show a working example, either. Given that I am still at the stage of needing examples to get PDFJS to function, I am at a loss as to how to make PDFJS stop trying to parse a problem PDF file and just move on to the next one.

For loops not working in Javasctipt for a declared variable from node.js in Jade

I am learning to work with node.js and JADE and am having problem with making the for loop work in a javascript within a jade file. I am trying to learn it with a simple code. I am passing an array from my database to the jade file. it works when I directly call a specific element of the array (var x = !{lat[2]}; alert(x), but it won't work when I put it in the for loop. here is my app.js code:
app.get('/test', function(req, res){
getlat(function(err, lat){
if(err){
return res.send(500);
}console.log(lat);
res.render('test', {lat:lat});
});
and this is where the lat is being retrieved from the database:
var mongoose = require ('mongoose');
function getlat(callback){
var lat = new Array();
mongoose.model('stories').find({},function(err, companies) {
if(err){
return callback(err, lat);
}
for (var i = 0; i < companies.length; i++) {
lat[i] = companies[i].Lat;
}
return callback(null, lat);
});
}
module.exports = getlat;
and here is my jade file:
body
button.btn.btn-info Click Here!
script.
$('button').click( function() {
for (var i = 0; i < 2; i++) {
var x = !{lat[i]};
alert(x);
});
});
I would really appreciate it if someone tells me where I am making a mistake.
Thanks
The issue is that lat only exists server-side while i only exists client-side.
Despite both using JavaScript, the 2 environments are still managed separately from each other, with all of the server-side code running to completion before the client-side code begins.
One option to resolve this is to output the entirety of lat to a client-side variable in the view, using JSON and JavaScript's shared syntax, and iterating over it client-side.
script.
$('button').click(function () {
var lat = !{ JSON.stringify(lat) };
for (var i = 0; i < 2; i++) {
// ...
}
});

Loading csv files content into dictionary of objects

I'm trying to load the content of the several CSV files into a new array. CSV files have a typical structure, with a label in the first row, and values (both string and real numbers) separated by commas. This part of code is responsible for loading the data for future use with Google Maps Api (not a problem for now, since I'm stuck on just loading the data). I would like to have a structure, in which I could call an element by it's name, that's why the var nodedata = {}; is created.
So the thing I totally don't get is why some part of the code is not being executed at all? console.log(nodedata); is empty, at least not in my Firefox console.
That's my attempt to the problem - links to the csv files are in the code.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js"></script>
<script src="http://jquery-csv.googlecode.com/files/jquery.csv-0.71.js"></script>
<script type="text/javascript">
var nodes = {};
var generation = {};
var nodedata = {};
$.get('https://dl.dropboxusercontent.com/u/25575808/energy/nodes.csv', function (response) {
nodes = $.csv.toObjects(response);
console.log(nodes);
});
$.get('https://dl.dropboxusercontent.com/u/25575808/energy/generation.csv', function (response) {
generation = $.csv.toObjects(response);
console.log(generation);
});
function getGeneration (nodename){
gen = 0;
for (var i = 0; i < generation.length; i++) {
if (generation[i].datetime == "2013-01-01 01:00"){
if (generation[i].node == nodename){
gen = gen + Number(generation[i]["output (MW)"])
}
}
}
return gen;
}
for (var i = 0; i < nodes.length; i++) {
nodedata[nodes[i].Node] = {
center: new google.maps.LatLng(nodes[i].Latitude,nodes[i].Longitude),
nodegen : getGeneration(nodes[i].Node)
}
}
console.log(nodedata);
I believe the problem you're having is unrelated to the usage of CSV data, rather it is the fact that the data is being loaded asynchronously.
You are executing 2 $.get() requests to load the files, which will take some time to download the files. The browser does not wait for them to finish before continuing through the rest of the code.
Therefore, it is possible for console.log(nodedate) to be executed before any data exists inside the nodes array.
An easy way to handle this is to stack your callback functions so that the first GET request completes -> run the 2nd GET request -> finally, run the processing code.
Check out this reorganization of the code: http://jsfiddle.net/Vr7sw/
(I removed the Google Maps line since I don't have the library loaded)
the problem is, the $.get requests are asynchronous (see jquery documentation), try to call to a function, into your callback body like this :
function nodesToJson(nodes) {
for (var i = 0; i < nodes.length; i++) {
nodedata[nodes[i].Node] = {
center: new google.maps.LatLng(nodes[node].Latitude,nodes[node].Longitude),
nodegen : getGeneration(nodes[i].Node)
}
}
console.log(nodedata);
}
$.get('https://dl.dropboxusercontent.com/u/25575808/energy/nodes.csv', function (response) {
nodes = $.csv.toObjects(response);
//when the request are ready, process the nodes
nodesToJson(nodes);
});

this._get is not a function - javascript oop and prototypes

I'm using something similar to NodeJS called bondi, it's build on the Firefox js engine.. Basically i'm getting this error and I believe it's due to the way i'm referencing "this" in the .Get function below.
Basically there is a tool called SFtpClient. It has the method of "Get", to list the contents of a folder, but I want to change the prototype for this with a drop in include file. I need to change it so that it
a/ retries several times when it fails, and b/ it has a recursive folder listing function.
So I used the prototype to change it - moved .Get to ._Get.
Can anyone see why I would be getting the error:
Jan 23 04:51:34 beta bondi: === this._Get is not a function --- Prio(6) Result(0x0) File(/home/nwo/approot/include/sftpclientenh
when I run the code below?
Thanks
SFtpClient.prototype._Get = SFtpClient.prototype.Get;
SFtpClient.prototype.Get = function(Folder, Retries){
//defaults
if(!Retries) Retries = 5;
if(!Folder) Folder = "~/";
//vars
var FileListing = [];
var connect = function(){
//TODO JRF 19.01.2012 : re-enable this when bondi is fixed
// this.HomeDirectory.replace(/\/?$/, "/");
FileListing = this._Get(Folder);
return true;
}
var i = 1;
do{
var res = false;
try {
res = connect();
}catch(e){
Debug.LogInfo(e.message);
}
i++;
Server.Sleep(i*2000);
} while(res==false && i < Retries);
return FileListing;
}
Try res = connect.call(this) instead of res = connect().

Categories

Resources