【问题标题】:Promise error when querying Stardog with NodeJS使用 NodeJS 查询 Stardog 时出现 Promise 错误
【发布时间】:2018-02-20 17:38:08
【问题描述】:

尝试从 Node JS 应用程序查询本地运行的 Stardog 数据库。

查询在 Stardog 界面中运行时返回结果。

在 Node 中运行它时,返回 null 和错误的 Promise 会出现问题。

(node:1248) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Cannot read property 'results' of null
(node:1248) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

我在Node中运行的代码是:

const { Connection, query } = require('stardog');

const conn = new Connection({
    endpoint: 'http://localhost:5820',
    auth: {
        user: 'admin',
        pass: 'admin'
    }
});

var q = 'select distinct ?s where { ?s ?p ?o }'

query.execute(conn, 'hospital_db', q, {
}).then(({ body }) => {
  console.log(body.results.bindings);
});

【问题讨论】:

    标签: node.js stardog


    【解决方案1】:

    你没有在 promise 中处理错误。

    query.execute(conn, 'hospital_db', q, {
    }).then(({ body }) => {
      console.log(body.results.bindings);
    }).catch((err) => {
      console.log(err);
    })
    

    【讨论】:

    • 谢谢,仍然返回 null 但这可能是另一个问题。
    【解决方案2】:

    你应该在你的承诺链中添加一个 catch。

     query.execute(conn, 'hospital_db', q, {
        }).then(({ body }) => {
            console.log(body.results.bindings);
        }).catch( (err) => {
            console.log(err);
        })
    

    【讨论】:

      【解决方案3】:

      Stardog.js 的 Connection 对象接受端点、用户名、密码和元对象,而您有一个 auth 对象。你可以在https://github.com/stardog-union/stardog.js#connectionoptions的文档中看到这一点

      【讨论】:

      • 删除auth obj时没有区别。
      猜你喜欢
      • 2018-09-09
      • 1970-01-01
      • 2016-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-11
      • 2011-07-28
      • 2020-04-19
      相关资源
      最近更新 更多