从Node中的级别db返回数据时,Hapijs抛出``无法抛出非错误对象错误''

时间:2018-08-05 09:12:55

标签: node.js hapijs leveldb

我一直在努力解决这个问题:

尝试:根据Hapijs中的GET请求从levelDB返回数据。 但是得到:

Debug: internal, implementation, error 
    Error: Cannot throw non-error object
    at module.exports.internals.Manager.execute (.../node_modules/hapi/lib/toolkit.js:42:33)
    at <anonymous>

首先应该没有错误。在Node REPL环境中检索数据可以正常工作。通过POST请求将数据放入数据库也可以正常工作。

LevelDB帮助程序函数获取和检索数据:

/** Helper functions to interact with the address levelDB */

const level = require('level');
const addressDB = './adressdata';
const db = level(addressDB);

function addAddressToDB(address) {
  return new Promise( (resolve, reject ) => {
    db.put(address.toString(), 'valid')
      .then( () => resolve('Stored Address.'))
      .catch(err => reject(err));
  });
}

// Checks how many stars an address still can create
function getAddressInfo(address) {
  return new Promise( (reject, resolve) => {
    db.get(address.toString())
      .then(value => resolve(value))
      .catch(err => reject(err));
  });
}

module.exports = {
  addAddressToDB: addAddressToDB,
  getAddressInfo: getAddressInfo
};

Hapijs服务器配置:

'use strict';

// Basic server setup
const Hapi = require('hapi');
const addressDB = require('./addressdb-utilities');

const server = Hapi.server({
    port: 8000,
    host: 'localhost'
});
// Helper to Add Address
  server.route({
    method: 'POST',
    path: '/addAddress',
    config: {
      handler: async (request, response) => {
        try {
          let address = request.payload.address;
          let response = await addressDB.addAddressToDB(address, 'valid')

          return response;
        } catch(err) { throw new Error(err) }
      }
    }
  });

  // Helper to retrieve address
  server.route({
    method: 'GET',
    path: '/address/{address}',
    handler: async (request, h) => {
      let address = encodeURIComponent(request.params.address);
      let status = await addressDB.getAddressInfo(address); // This line causes the error
      console.log('Address: ', address);
      console.log('Address Type: ', typeof address);
      console.log(status);
      return 'status';
    }
  });
// Initialize server
const init = async () => {
    await server.start();
    console.log(`Server running at: ${server.info.uri}`);
    return;

};

// Error handling
process.on('unhandledRejection', (err) => {

    console.log(err);
    process.exit(1);
});

init();

将所有内容包装在try / catch博客中不会导致服务器上显示任何错误消息,但会根据请求提供代码500响应。解决错误和价值也不会改变任何事情。

我非常感谢每个提示提供更多调试信息,请尝试aso。我没有想法,到目前为止研究仍无济于事。希望您能在这里找到所有提供的信息。

环境:

  • 节点版本:8.11.3
  • Hapi版本:17.5.3
  • 级别版本:4.0.0
  • OS:OSX 10.13.6(塞拉利昂)

1 个答案:

答案 0 :(得分:0)

调试许多小时后。我意识到,promise会按错误的顺序排列……因此拒绝解决,反之亦然。