Connecting to ravendb using node.js

250 Views Asked by At

I'm currently trying to set up a small project with node.js and ravendb. But when I try to connect to the ravendb test server or a local server I get the following error:

(node:20336) UnhandledPromiseRejectionWarning: AllTopologyNodesDownException: Tried to send BatchCommand request via POST http://live-test.ravendb.net/databases/Northwind/bulk_docs to all configured nodes in the topology, all of them seem to be down or not responding. I've tried to access the following nodes: http://live-test.ravendb.net: socket hang up
at getError (C:\...\node_modules\ravendb\dist\Exceptions\index.js:17:19)
at Object.throwError (C:\...\node_modules\ravendb\dist\Exceptions\index.js:13:11)
at RequestExecutor._throwFailedToContactAllNodes (C:\...\node_modules\ravendb\dist\Http\RequestExecutor.js:669:22)
at RequestExecutor.<anonymous> (C:\...\node_modules\ravendb\dist\Http\RequestExecutor.js:573:26)
at Generator.next (<anonymous>)
at fulfilled (C:\...\node_modules\ravendb\dist\Http\RequestExecutor.js:4:58)
at process._tickCallback (internal/process/next_tick.js:68:7) 
(node:20336) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). 
(rejection id: 1) 
(node:20336) [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.

Index.js

const { DocumentStore } = require('ravendb');

const store = new DocumentStore("http://live-test.ravendb.net", "Northwind");

store.initialize();   

const session = store.openSession();

async function asyncFunction() {
    let test = {
        name: 'abc',
        value: '123',
    };

    await session.store(test, 'test/');
    console.log(test.id);
    await session.saveChanges();
}

asyncFunction()

store.dispose();

package.json

{
  "dependencies": {
    "ravendb": "^4.1.5"
  }
}
2

There are 2 best solutions below

0
Abderrahim Soubai-Elidrisi On

Try like this :

import { DocumentStore } from "ravendb";

const store = new DocumentStore(["http://live-test.ravendb.net"],"Northwind");                       

const conventions = store.conventions;  

store.initialize();  
0
gregolsky On

Can you try changing last 2 lines to:

asyncFunction()
    .then(() => store.dispose());

You are not awaiting async function call and disposing DocumentStore immediately after asyncFunction() is called.

DocumentStore.dispose() should be called on the application shutdown. It cleans caches and closes open connections.