简体   繁体   English

我可以使用 Javascript 查询 neo4j 数据库吗?

[英]Can I query the neo4j database with Javascript?

I have created a geohash neo4j database for NYC Taxi data.我为纽约出租车数据创建了一个 geohash neo4j 数据库。 Now the next step is to visualize it within a map, for that i choosed Leaflet as a Javascript library.现在下一步是在地图中可视化它,为此我选择 Leaflet 作为 Javascript 库。 with static data i can plot geohash data in Leaflet:使用静态数据,我可以在 Leaflet 中绘制 geohash 数据:

geohash-传单

but now i want to query that data from the neo4j database and render it.但现在我想从 Neo4j 数据库中查询该数据并呈现它。

so is it possible to do that or only with a server side scripting language(node.js,php...) ?那么有可能做到这一点还是只能使用服务器端脚本语言(node.js,php...)?

Update更新

i found a simlair question here , the solution is to query the database with ajax however it dosen't work for me and i get "error" in the console:我在这里找到了一个 simlair 问题,解决方案是使用 ajax 查询数据库,但是它对我不起作用,我在控制台中收到“错误”:

var body = JSON.stringify({
            statements: [{
                statement: 'MATCH (n) RETURN count(n)'
            }]
        });
   $.ajax({
        url: "http://localhost:7474",
        type: "POST",
        data: body,
        contentType: "application/json"
    })
        .done(function(result){
            console.log(result);

        })
        .fail(function(error){
            console.log(error.statusText);
        });

It's possible to query Neo4j from client-side Javascript with Neo4j Driver for JavaScript .可以使用Neo4j Driver for JavaScript从客户端 Javascript 查询 Neo4j。

I have used this in a couple of projects.我在几个项目中使用过它。

You can either download the driver and include in your HTML file like:您可以下载驱动程序并将其包含在您的 HTML 文件中,例如:

 <script src="lib/browser/neo4j-web.min.js"></script>

Or just use the CDN link like:或者只使用 CDN 链接,如:

<script src="https://unpkg.com/neo4j-driver@X.Y.Z/lib/browser/neo4j-web.min.js"></script>

I find the solution:我找到了解决方案:

first the url for the database is : " http://localhost:7474/db/data/transaction/commit " and not " http://localhost:7474 ".首先数据库的 url 是:“ http://localhost:7474/db/data/transaction/commit ”而不是“ http://localhost:7474 ”。

then after changing that i got an unauthorized error in console , that means i need to add my user/password to my ajax call, this is done by a function called beforeSend like this:然后在更改后我在控制台中遇到未经授权的错误,这意味着我需要将我的用户/密码添加到我的 ajax 调用中,这是由一个名为 beforeSend 的函数完成的,如下所示:

beforeSend: function (xhr) {
                xhr.setRequestHeader ("Authorization", "Basic " + btoa("neo4j"+ ":" + "your_neo4j_password"));
            }}

so the final Ajax Solution is :所以最终的 Ajax 解决方案是:

$.ajax({
            url: "http://localhost:7474/db/data/transaction/commit",
            type: "POST",
            data: body,
            contentType: "application/json",
            beforeSend: function (xhr) {
                xhr.setRequestHeader ("Authorization", "Basic " + btoa("neo4j"+ ":" + "password"));
            }}
            )
            .done(function(result){
                 console.log(result);
            }) 
            .fail(function(error){
                console.log(error.statusText);
            });

Rajendra Kadam's answer is correct. Rajendra Kadam 的回答是正确的。

First you need to install neo4j-driver by:首先,您需要通过以下方式安装neo4j-driver

npm install neo4j-driver

in a directory probably one level higher than the web/ directory of your node.js server.在一个可能比node.js服务器的web/目录高一级的目录中。

Then you need to put the neo4j-web.min.js into the web/ directory, where your client-side JavaScript can load.然后你需要把neo4j-web.min.js放到web/目录,你的客户端 JavaScript 可以在那里加载。

Then you add the line in your HTML:然后在 HTML 中添加以下行:

 <script src="js/neo4j-web.min.js"></script>

The file neo4j-web.min.js is located in node_modules/neo4j-driver/lib/browser/ .文件neo4j-web.min.js位于node_modules/neo4j-driver/lib/browser/

Then in your client-side JavaScript, put:然后在您的客户端 JavaScript 中,输入:

var driver = neo4j.driver(
    'neo4j://localhost',
    neo4j.auth.basic('neo4j', 'password') );

Then you have successfully opened the driver.那么你已经成功打开了驱动程序。 (You may need to set your password correctly or you'll get an authentication error.) (您可能需要正确设置密码,否则会出现身份验证错误。)

Note that you DON'T need this line on your client-side JavaScript:请注意,您不需要在您的客户端JavaScript这行:

var neo4j = require('neo4j-driver');

because it is for server-side node.js .因为它用于服务器端node.js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM