Unable to establish a neo4j - bolt driver connection in javascript - javascript

I am trying to create a connection between my html embedded javascript and my neo4j database by running the index.html in Chrome. I have reduced the source of the problem to 'neo4j' not being recognised. So the error thrown will be of the type:
Cannot read property ['driver'/'basic'/etc...] of undefined.
In this case I have assumed that 'undefined' is referring to 'neo4j', which would mean that I am not implementing 'neo4j-web.min.js' correctly.
The below block of code is extracted from my index.html and has been taken from: https://www.npmjs.com/package/neo4j-driver
<script src="node_modules/neo4j-driver/lib/browser/neo4j-web.min.js"></script>
<script type="text/javascript" charset="utf-8">
var driver = neo4j.driver("bolt://localhost:7474", neo4j.auth.basic(neo4j,
neo4j));
</script>
Given that the issue seems very localised to this code, I spared everyone the rest of the document. If further context is missing, I'd be happy to provide it.

The neo4j-driver module uses an odd system whereby you have to specify which version of the API you want to use.
<script src="node_modules/neo4j-driver/lib/browser/neo4j-web.min.js"></script>
<script type="text/javascript" charset="utf-8">
neo4j = neo4j.v1
var driver = neo4j.driver("bolt://localhost:7474", neo4j.auth.basic(neo4j,
neo4j));
</script>

I agree with #varbrad
My 2cts: check the servername
You are using the alias localhost

Related

protobuf.js using CommonJS?

I am relatively new to JS but very familiar with protobuf. I'm currently designing a web page hosted from a Java HTTP server, and would like to implement protobuf communication between them.
My issue is on the browser side. After some research I found the protobuf.js git page and attempted to use this within my javascript. I ran into issues firstly getting the module over HTTP because
<script src="//cdn.rawgit.com/dcodeIO/protobuf.js/6.X.X/dist/protobuf.js"></script>
uses text/plaintext and fails to return. Adding a type=text/javascript just led to protobuf is not defined.
I then tried to take the project source into my web root, and directly use this:
<script type="text/javascript" src="./js/protobuf-js/src/index.js" ></script>
and:
import * as protobuf from "./js/protobuf-js/src/index.js";
This worked and the web server returned the file. Now, this is where my understanding reaches it's limits. From what I can tell from the README page on git, it distinctly says
"The library supports CommonJS and AMD loaders and also exports globally as protobuf."
If I look inside index.js I see the following:
var protobuf = module.exports = require("./index-light");
which throws a Module is not defined in ES module scope exception in browser.
Nowhere else online could I find working examples of the protobuf.js being used in commonJS as it states in the git, it all refers to Node.js which I don't want to use as i'm using Java for the webserver side of things.
Am i being really dumb and missing something obvious?
Thanks
There are example in https://github.com/protobufjs/protobuf.js.
a small example:
hello.proto
syntax = "proto3";
message Test{
string msg=1;
}
test.html
<html lang="en">
<head>
<script src="//cdn.rawgit.com/dcodeIO/protobuf.js/6.11.3/dist/protobuf.js"></script>
</head>
<body>
<script>
function test(){
protobuf.load("hello.proto", function(err, root) {
var TestMsg = root.lookup('Test');
var payload = {msg:'hello'};
var result = TestMsg.verify(payload);
if(result) throw Error(result);
var msg = TestMsg.create(payload);
var binMsg = TestMsg.encode(msg).finish(); // this is the binary protobuf message
// to handle blob data from server via websocket, you need handle like below
// event.data.arrayBuffer().then(buf =>{
// var msg = TestMsg.decode(new Uint8Array(buf));
// }
// deserialize
var msg2 = TestMsg.decode((binMsg));
console.log(msg2.toJSON());
alert(msg2.msg);
});
}
</script>
<input type="button" value="test" onclick="test()">
</body>
</html>

Writing a javascript generated file using File API

I would like to use the File API in Javascript to be able to store generated content on the client browser's file system. I've looked at the documentation, but found no cross-browser solution (FileSystem API appears to be Chrome only).
With File API: Writer being discontinued, I don't know what options I have. Any suggestions?
You are correct that the FileSystem API is only supported in Chrome at the time of this writing, and it will probably not be implemented in other browsers.
http://caniuse.com/#feat=filesystem
Per MDN, the FileSystem API shouldn't be used in production:
This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.
At the moment better supported options to store files on the client are:
Web Storage API - http://caniuse.com/#feat=namevalue-storage
Cookies
IndexedDB - http://caniuse.com/#feat=indexeddb
WebSQL database - http://caniuse.com/#feat=sql-storage
I found the following solution that is simple and appears to solve my problem well. With this, I can generate a file and offer a download like link to the user to save. Here is the example code I use:
HTML
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<textarea id="fileContent" type="text" cols=80 rows=30>File Content!</textarea>
<a id="anchor" href="#" download="fileContent.txt">Download Me!</a>
<script src="clientfile.js"></script>
</body>
</html>
JavaScript
var fileContent = document.getElementById("fileContent");
var anchor = document.getElementById("anchor");
anchor.onclick = function () {
var fileContent_blob = new Blob([fileContent.value], {type: 'text/plain'});
if (window.navigator.msSaveBlob === undefined) {
anchor.setAttribute('href', URL.createObjectURL(fileContent_blob));
}
else {
window.navigator.msSaveOrOpenBlob(fileContent_blob, 'fileContent.txt');
}
}

Looking for syntax error in js code a vendor sent me to use

I am a designer and am producing an html5 banner for my client using Edge. The adserver they are using requests that this simple javascript be inserted into the html to pass some tracking info. But Dreamweaver says there is a syntax error whenever i add the js. Here it is:
<script type="text/javascript">
var clickTAG = "<!mpvc/>http://<!mpck/>";
document.location = clickTAG;
</script>
Can anyone tell me what the syntax error is? Thank you!
Looks like that those semicolons aren't real semicolons, they're Greek Question Marks (hex 0x037e). Change them to normal semicolons, and it should run fine.
var clickTAG = "<!mpvc/>http://<!mpck/>";
document.location = clickTAG;

Handling .js and .css in Node.js

I'm trying to create a simple chat application with node.js and jQuery but I'm getting an error in my console saying '$ is not defined'. I'm following a tutorial where the authr is using jquery without a problem.
My chat.js (the server):
var http = require('http'),
sys = require('sys'),
fs = require('fs'),
ws = require('./ws.js');
var clients = [];
http.createServer(function(req,res){
res.writeHead(200,{
'Content-Type': 'text/html'
});
var rs = fs.createReadStream(__dirname + '/test.html');
sys.pump(rs, res);
}).listen(4000);
ws.createServer(function(websocket){
websocket.on('connect', function(resource){
clients.push(websockets);
websocket.write('Welcome');
});
websocket.on('data', function(data){
clients.forEach(function(client){
client.write(data);
});
});
websocket.on('close',function(){
var pos = clients.indexOf(websocket);
if(pos >= 0){
clients.splice(pos, 1);
}
});
}).listen(8081);
test.html (the client):
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(e) {
ws = new WebSocket("ws://localhost:8080");
ws.onmessage = function(event){
var data = data.replace(/&/g, "&").replace(/</g,"<").replace(/>/g,">");
$('#log ul').append('<li>' + data + '</li>');
}
});
</script>
</head>
<body>
<div id="log"><ul></ul></div>
<div id="console">
<input type="text" id="entry"/>
</div>
</div>
</body>
</html>
I was getting errors before I stripped out all the .css and other .js src so I'm pretty sure the problem is with the 'Content-Type' code in the server.
I suspect the problem is that you are not serving jquery-1.7.2.min.js from your server, so the web page is not loading jquery, hence no $. First, try referencing a hosted jquery and see if that fixes your problem.
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
Although not totally relevant to your problem, if you are building a public website, then it is generally a good idea to reference hosted jquery (and other libs) as your users will probably have a locally cached copy already (thanks to other sites which did the same thing). See http://encosia.com/3-reasons-why-you-should-let-google-host-jquery-for-you/ for a reasonable explanation of this.
Generally, though, you are going to want to serve some static files: your own css and js, images and the like. There are --plenty--of--posts-- describing how to do this longhand with node, but you may want to look at a dedicated module like this: https://github.com/cloudhead/node-static
On the server side, the code you are using is very old. sys.pump has been deprecated for at least over a year.
I'd advise you to read the examples from socket.io. There happens to be a chat server as well in the examples.
Who's serving your static files? From the code you have, it looks like the only static file you're serving is the HTML page.
You need to add some code to handle all static files at a specific location. Basically, you need to handle all static files within your
http.createServer(function() {...})
See: http://www.sitepoint.com/serving-static-files-with-node-js/

Can Indy run Javascript?

There is a software product called AnyChart which is great for embedding Flashed based charts in web pages. AnyCharts can also export to PNG file format. Here is an example:
<script type="text/javascript" language="javascript">
//<![CDATA[
var chart = new AnyChart('http://www.mysite.com/swf/AnyChart.swf');
chart.width = 600;
chart.height = 300;
chart.setXMLFile('http://www.mysite.com/anychart.xml');
chart.addEventListener("draw", function() { saveChartAsImage(chart); });
chart.write("content-box");
//]]>
</script>
My ultimate goal is to make a automated service to export the AnyChart charts to PNG format. So I made a service with Indy which calls pages containing the AnyChart javascript. But the problem seems to be that Indy cannot execute the javascript.
Is there a way to enable Indy to execute javascript?
No, Indy does not execute Javascript. You may have also noticed that it doesn't parse or display HTML, and it doesn't run Flash, either. Indy does network protocols.
You could import the Microsoft Script Control ActiveX object and have that run your Javascript. If you need details on that, post a new question.
You don't have to use Indy for this. If you want you can use TWebBrowser.
IHTMLWindow2 interface has execScript function. So may be you can :
var
Doc : IHTMLDocument2;
Win : IHTMLWindow2;
aBrowser : TWebBrowser;
//...
begin
//...
Doc := aBrowser.Document as IHTMLDocument2;
Win := Doc.parentWindow;
Win.execScript('alert(SomeMessage);', 'JavaScript');
end;
Did you try vcl FOR THE web (aka Intraweb atozed) ?
There is a teechart version wich is quite useful, you can also execute "external" javascript code within any of the TiwForms of your web app (the exact same code you are using now).
Post a new question if you need to and I'll be glad to help.

Categories

Resources