简体   繁体   English

具有错误处理的Node.js Oriento驱动程序示例

[英]Node.js Oriento driver example with error handling

I'm new to node.js and trying to teach myself node with orientdb as the datastore. 我是node.js的新手,并试图通过orientdb作为数据存储来教自己节点。 I'm using oriento as the driver. 我正在使用oriento作为驱动程序。

I want to be able to handle any connection errors in my code but cannot find any examples of how to do this during the initial connection to orientdb server and connection to my database: 我希望能够处理代码中的任何连接错误,但在与Orientdb服务器的初始连接以及与数据库的连接期间找不到任何如何执行此操作的示例:

var express = require('express');
var oriento = require('oriento');

var server = oriento({
    host: "localhost",
    port: 2424,
    username: "root",
    password: "test"
});

How do I catch if there is connection error to the server? 如果服务器连接错误,如何捕获? Later on when I want to work with a specific database in the server: 稍后,当我想使用服务器中的特定数据库时:

var db = server.use({
    name: 'blog',
    username: 'admin',
    password: 'admin'
});

How do I catch if there is an error in "use"ing this database? 如果“使用”该数据库时出现错误,如何捕获?

I can sort of figure out from the oriento documentation how to handle errors during queries etc., but got stuck in these initial steps. 我可以从oriento文档中弄清楚如何处理查询等期间的错误,但是陷入了这些初始步骤。

Here is how. 这是怎么回事。 Basically, it does auto connect to the server on the first request. 基本上,它会在第一个请求时自动连接到服务器。 For connection errors you need catch on every request. 对于连接错误,您需要catch每个请求。

var Oriento = require('oriento');

var server = Oriento({
    host: 'localhost',
    port: 2424,
    username: 'root',
    password: 'BDFE8AC356595663AF66ADF08E703DE30DF5755F99DE9D329EFF75A5CB8A9CE8'
});

server
    .list()
    .then(function (dbs) {
        console.log('There are ' + dbs.length + ' databases on the server.');

        var firstDB = dbs[0];

        var orientDB = server.use({
            name: firstDB.name,
            username: firstDB.username,
            password: firstDB.password
        });

        console.log('Using database: ' + orientDB.name);
    })
    .catch(function (err) {
        console.log(err);
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM