【问题标题】:Errors in importing Neo4j Javascript Driver to Angular 2 CLI将 Neo4j Javascript 驱动程序导入 Angular 2 CLI 时出错
【发布时间】:2018-10-10 14:28:49
【问题描述】:

你好, 在我的 Angular 2 CLI 应用程序中尝试使用 Neo4j 驱动程序时,我遇到了一些 module not found 错误。

我可能在尝试导入它时遗漏了一些东西。

  • 我安装了它:ng install neo4j-driver
  • 在我的服务中.ts:import 'neo4j-driver/lib/browser/neo4j-web';
  • 尝试在服务中调用此示例代码:

    getFromDB(): void {
       var neo4j = require('neo4j-driver').v1;
       var driver = neo4j.driver("bolt://localhost", neo4j.auth.basic("neo4j", "pisikopat"));
    
       // Create a session to run Cypher statements in.
       // Note: Always make sure to close sessions when you are done using them!
       var session = driver.session();
    
       // Run a Cypher statement, reading the result in a streaming manner as records arrive:
       session
         .run("OPTIONAL MATCH (source)-[r]-(target) WHERE r is not null RETURN (source)")
         .subscribe({
           onNext: function (record) {
             console.log(record._fields);
           },
           onCompleted: function () {
             // Completed!
             session.close();
           },
           onError: function (error) {
             console.log(error);
           }
         });
     }
    

当我ng serve我的应用程序时,我收到以下错误:

ERROR in ./~/neo4j-driver/lib/v1/internal/ch-node.js
Module not found: Error: Can't resolve 'net' in 'E:\emek\node_modules\neo4j-driver\lib\v1\internal'
 @ ./~/neo4j-driver/lib/v1/internal/ch-node.js 32:11-25 364:2-24
 @ ./~/neo4j-driver/lib/v1/internal/connector.js
 @ ./~/neo4j-driver/lib/v1/driver.js
 @ ./~/neo4j-driver/lib/v1/index.js
 @ ./~/neo4j-driver/lib/index.js
 @ ./src/app/services/heroes.service.ts
 @ ./src/app/app.component.ts
 @ ./src/app/index.ts
 @ ./src/main.ts
 @ multi main

ERROR in ./~/neo4j-driver/lib/v1/internal/ch-node.js
Module not found: Error: Can't resolve 'tls' in 'E:\emek\node_modules\neo4j-driver\lib\v1\internal'
 @ ./~/neo4j-driver/lib/v1/internal/ch-node.js 36:11-25
 @ ./~/neo4j-driver/lib/v1/internal/connector.js
 @ ./~/neo4j-driver/lib/v1/driver.js
 @ ./~/neo4j-driver/lib/v1/index.js
 @ ./~/neo4j-driver/lib/index.js
 @ ./src/app/services/heroes.service.ts
 @ ./src/app/app.component.ts
 @ ./src/app/index.ts
 @ ./src/main.ts
 @ multi main

ERROR in ./~/neo4j-driver/lib/v1/internal/ch-node.js
Module not found: Error: Can't resolve 'readline' in 'E:\emek\node_modules\neo4j-driver\lib\v1\internal'
 @ ./~/neo4j-driver/lib/v1/internal/ch-node.js 92:2-21
 @ ./~/neo4j-driver/lib/v1/internal/connector.js
 @ ./~/neo4j-driver/lib/v1/driver.js
 @ ./~/neo4j-driver/lib/v1/index.js
 @ ./~/neo4j-driver/lib/index.js
 @ ./src/app/services/heroes.service.ts
 @ ./src/app/app.component.ts
 @ ./src/app/index.ts
 @ ./src/main.ts
 @ multi main

angular-cli: 1.0.0-beta.15 节点:4.5.0 操作系统:win32 x64

【问题讨论】:

    标签: angular neo4j angular2-cli


    【解决方案1】:

    这可能有点晚了,但无论如何。

    首先,您需要更改导入和分配驱动程序的方式。 变化:

    import 'neo4j-driver/lib/browser/neo4j-web';
    

    收件人:

    import * as neo4j_driver from 'neo4j-driver/lib/browser/neo4j-web.min.js';
    

    然后更改节点查找语法:

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

    收件人:

    const neo4j = neo4j_driver.v1;
    

    这将消除您遇到的错误。

    那么您实际上可能会因为未在 uri 中分配端口而收到错误消息。为了获得正确的端口(7474 对我不起作用),我启动了数据库并在 localhost:7474 访问 Neo4j 浏览器。然后,您必须查看显示“您以用户 neo4j 身份连接的页面” 到服务器 bolt://localhost:7687" 我不确定这是默认设置还是已分配,但无论如何。

    然后我在我的 uri 中使用了该端口,现在一切都在 Angular 内部工作了(尽管如果使用远程连接(例如 Graphene)并且我能找到的唯一文档不适用于 JS 驱动程序,我无法让它工作)。

    这是我的完整解决方案:

    import {Component, OnInit} from '@angular/core';
    import * as neo4j_driver from 'neo4j-driver/lib/browser/neo4j-web.min.js';
    
    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css']
    })
    export class AppComponent implements OnInit {
      title = 'app works!';
    
      constructor() {
      }
    
      ngOnInit() {
        const neo4j = neo4j_driver.v1;
        const uri = 'bolt://localhost:7687';
        const user = 'neo4j';
        const password = '<your password>';
        const driver = neo4j.driver(uri, neo4j.auth.basic(user, password), {maxTransactionRetryTime: 15000});
    
        // Register a callback to know if driver creation was successful:
        driver.onCompleted = function () {
        // proceed with using the driver, it was successfully instantiated
        };
        // Register a callback to know if driver creation failed.
        // This could happen due to wrong credentials or database unavailability:
        driver.onError = function (error) {
          console.log('Driver instantiation failed', error);
        };
    
        const session = driver.session();
        const result = session.run('MATCH (a:Person) RETURN a.name ORDER BY a.name');
        const people = [];
    
        result.subscribe({
          onNext: record => {
            const name = record.get(0);
            people.push(name);
          },
          onCompleted: () => {
            session.close();
            console.log(people);
          },
          onError: error => {
            console.log(error);
          }
        });
      }
    }
    

    【讨论】:

    • 我在这里 - 找不到名称“代码”。请帮忙
    • 这是一个错字 - 应该是“people.push”。已更新答案
    • 顺便说一句,你不应该在生产/面向公众的应用程序中使用它
    • 嗨 Epiphanatic,你能再帮我一次吗。我想在我的 HTML 中显示输出。怎么做。这是我的 angular2 和 nodejs 代码 --> stackoverflow.com/questions/52741184/… 和这是 NodeJS 社区提供的 javascript 代码,我想在我的 angular 和 nodejs 应用程序中翻译它 - stackoverflow.com/questions/52681565/…
    猜你喜欢
    • 1970-01-01
    • 2019-09-21
    • 1970-01-01
    • 2023-03-16
    • 2017-12-28
    • 2018-11-07
    • 1970-01-01
    • 1970-01-01
    • 2017-12-02
    相关资源
    最近更新 更多