Node.js和ArangoDB查询在单个函数调用上执行多次?

时间:2018-08-03 01:56:22

标签: node.js docker arangodb hapijs arangojs

我有一个使用ArangoDB的节点应用程序,当我查询时,我注意到日志中有大量重复的条目。我不知道它是在执行多个请求,还是只是反复记录。

我正在从我的Hapi.js处理程序中调用它,这是一个异步函数

let user = await arango.getUser('user1');

这是功能

exports.getUser = function(name) {
  return new Promise(function(fulfill, reject) {
    let query = `FOR u IN users FILTER u._key == '${name}' RETURN u`
    db.query(query).then(user => {
      logger.debug(`Retrieved user '${name}' successfully.`);
      fulfill(user._result[0]);
    }).catch(err => {
      logger.error(`Failed to query for user '${name}'`);
      reject();
    });
  });
}

它实现单个结果,但是在单个页面加载时日志看起来像这样。总是16次。

2018-08-03T01:33:17.922Z debug: Retrieved user 'user1' successfully.
2018-08-03T01:33:17.925Z debug: Retrieved user 'user1' successfully.
2018-08-03T01:33:18.006Z debug: Retrieved user 'user1' successfully.
2018-08-03T01:33:18.027Z debug: Retrieved user 'user1' successfully.
2018-08-03T01:33:18.030Z debug: Retrieved user 'user1' successfully.
2018-08-03T01:33:18.036Z debug: Retrieved user 'user1' successfully.
2018-08-03T01:33:18.041Z debug: Retrieved user 'user1' successfully.
2018-08-03T01:33:18.042Z debug: Retrieved user 'user1' successfully.
2018-08-03T01:33:18.045Z debug: Retrieved user 'user1' successfully.
2018-08-03T01:33:18.046Z debug: Retrieved user 'user1' successfully.
2018-08-03T01:33:18.053Z debug: Retrieved user 'user1' successfully.
2018-08-03T01:33:18.057Z debug: Retrieved user 'user1' successfully.
2018-08-03T01:33:18.058Z debug: Retrieved user 'user1' successfully.
2018-08-03T01:33:18.147Z debug: Retrieved user 'user1' successfully.
2018-08-03T01:33:18.204Z debug: Retrieved user 'user1' successfully.
2018-08-03T01:33:18.293Z debug: Retrieved user 'user1' successfully.

我不确定如何调试它以查看Arango实际被查询了多少次,或者它是否在等待承诺解决的同时在.then()中执行代码。我从未见过这种行为。我在MacBook上的Docker中运行Docker中的Arango,并在终端中运行Node。如果它实际上没有执行查询16次,并且只是异步函数的良性副作用,那很好,我只是想确保我在这里没有做错什么。感谢您的帮助。

edit:在进行额外的测试后,如果在Hapi.js处理程序之外而不是在页面加载期间调用该函数,则日志仅弹出一次。

0 个答案:

没有答案