how to fix these when using neo4j_driver in nodejs? - javascript

I am having several problems when trying to use neo4j in nodejs at the backend.
The following seems fine, but I could not see any nodes in local database browser.
var neo4j = require('neo4j-driver').v1;
var driver = neo4j.driver("bolt://localhost", neo4j.auth.basic("neo4j", "neo4j"));
var session = driver.session();
session.run('CREATE (a:person {name: "aaaa")-[a:work_at]->(b:company {type:"Inc"})');
session.close();
driver.close();
at the local browser
http://localhost:7474/browser/
I tried to see these added nodes by
match (a) return a
but nothing came out.
So, where above nodes are now? how do I know that i added something to database?
Since above code seems fine, I put it inside a function in a module, called it in another module. The problem is that I got an error, how is this possible? It is the same code, OMG!
function test() {
var neo4j = require('neo4j-driver').v1;
var driver = neo4j.driver("bolt://localhost", neo4j.auth.basic("neo4j", "neo4j"));
var session = driver.session();
session
.run( 'CREATE (a:person {name: "aaaa")-[a:work_at]->(b:company {type:"Inc"})' )
.then( () => {
console.log( 'add a node' );
session.close();
driver.close();
})
.catch(error =>{
console.log( error );
session.close();
driver.close();
return error;
})
}
I keep getting the following error. Searched everywhere, but could not fix it.
Error: Connection was closed by server
How can I specify where to put my database files, name of the database file and so on?
I wanted to import data to noe4j from csv file.
Can I use 'load csv' like the following
session.run('load csv from .....')
can I do that?
I saw every one just using 'load csv' from commend line.
If I must have to do from command line, how can I specify the path of the file?
Anyone point out what I am doing wrong?
Please help!

Your Cypher query has a couple of major problems, and will not work.
You omitted the } when assigning the name property to the person node.
You are attempting to use the same identifier, "a", in two conflicting ways: for a node and for a relationship.
Something like this would work (I left out all identifiers, since your current query doesn't need them):
CREATE (:person {name: "aaaa"})-[:work_at]->(:company {type:"Inc"})

Related

Accessing keepass databass returns KdbxError: Error BadSignature

I am using the kdbxweb library.
My goal is to open a kdbx database file, and then retrieve a password from it.
Following the example on the page, and also inspired by some things I saw in the keepass code, which uses this lib, I came up with this:
const password = kdbxweb.ProtectedValue.fromString('secret');
const credentials = new kdbxweb.Credentials(password);
const file = kdbxweb.ByteUtils.arrayToBuffer(
kdbxweb.ByteUtils.base64ToBytes('/home/chai/code/Kairos/src/e2e/db.kdbx'),
);
const db = await kdbxweb.Kdbx.load(file, credentials);
Sadly when I run it it gives me : Error | KdbxError: Error BadSignature
The file and password are correct; I verified that using the keepass application, which will open it without issue.
Any ideas are welcome! Thx!
Okay I found out what the problem was. It was in the structure of the keepass kdbx file. It was generated by importing a csv, but somehow all entries were directly under root. This gave me errors. Now restructuring it with a Group "db" (as per default) and then putting the entries under that solved the issue.

Node.js pg-promise Azure function to write to Postgres (timescaleDB)

Azure is driving me mad again. What I try to achieve is that the data that comes in through an Event Hub needs to be written to the database. What I got working thus far is that the data arrives at the Event Hub and that the Azure function is able to post data to the database. I would prefer to do this with Node.JS as the integration seems kind of nice in Azure. The script I use to send some bogus data to the database is as follows:
module.exports = async function (context, eventHubMessages){
const initOptions = {
query(e) {context.log(e.query)},
capSQL: true
//capSQL: true // capitalize all generated SQL
};
const pgp = require('pg-promise')(initOptions);
const db = pgp({
host: '####',
user: '####',
password: '####',
database: 'iotdemo',
port: 5432,
ssl: true
});
// our set of columns, to be created only once (statically), and then reused,
// to let it cache up its formatting templates for high performance:
const cs = new pgp.helpers.ColumnSet(['customer', 'tag', 'value', 'period'], {table: 'testtable'});
// generating a multi-row insert query:
const query = pgp.helpers.insert(JSON.parse(eventHubMessages), cs);
//=> INSERT INTO "tmp"("col_a","col_b") VALUES('a1','b1'),('a2','b2')
// executing the query:
db.none(query);
};
And yes, this is a snippet from somewhere else. The 'eventHubMessages' should contain the payload. A couple of issues that I have had thus far are:
I can send a payload defined within the script or whilst giving it a testing payload, but I cant send the payload of the actual message
pg-promise returns a 202 regardless of whether it fails or not, so debugging is 'blind' at the moment. Any tips on how to get proper logging would be much appreciated.
I used 'capture events' in the event hub instance to capture the actual messages. These were stored in a blob storage. In noticed that the format is Avro. Do I need to peel away at that object to get to the actual array?
The input should look something like this:
[{"customer": duderino, "tag": nice_rug, "value": 10, "period": 163249839}]
I think I have 2 issues:
I dont know how to get meaningful logging out of the Azure function using Node.JS
Something is off about how my payload is coming in.
A more deeper question is, how do I know whether the Azure function is getting the data that it should. I know that the Event Grid gets the data, but there is no throughput. Namespaces are consistent and the Azure Function should be triggered by that namespace and get the input as a string.
I am seriously lost and out of my depth. Apart from the solution I would also appreciate feedback on my request. I am not a pro on StackOverflow and don't want to waste your time.
Regards
Ok, so after some digging I found a few things to resolve the issue. First of all, I was receiving the payload as a string, meaning that I needed it to parse first, before I could make it a callable object. In terms of code its simple, and part of the base functions of node.js
var parsed_payload = JSON.parse(payload_that_is_a_string);
Lastly, to get meaningful logging I found that the PG-Promise module has great support for that, and that this can be configured when loading the module itself. I was particularly interested in errors, so I enabled that option like so:
const initOptions = {
query(e) {console.log(e.query)},
capSQL: true,
//capSQL: true // capitalize all generated SQL
error: function (error, e) {
if (e.cn) {
// A connection-related error;
// console.log("DC:", e.cn);
// console.log("EVENT:", error.message);
}
}
};
That then can be used as a settings object for loading PG-Promise:
const pgp = require('pg-promise')(initOptions);
Thanks for considering my ask for help. I hope this proves useful for anyone out there!
Regards Pieter

Is this way to access mongodb in node.js acceptable?

I am new to programming and trying to experiment a bit, still struggling with the best way to access mongoDB from within my code. I've seen a few posts here on stack overflow but they more or less all require that the code required to load mongo is included in each and every .js file. I would like to avoid that in order to keep the code for accessing my DB in only one file.
Note that I am using the "mongo-factory" module.
Would the code below be acceptable?
I've created what I would call a "producer" of database objects, database.js
var mongoFactory = require('mongo-factory');
function Database(close,callback) {
mongoFactory.getConnection(<connection string>).then(function (database) {
callback(database.db(<db name>));
if(close) database.close();
}).catch(function (err) {
console.error(err);
});
}
module.exports = Database;
Then when I want to access the database from any of my files I could do the below, avoiding to introduce db-specific parameters and the mongo-factory requirement in here:
var Database = require('./database');
var callback_actOnDatabase = function (db) {
db.collection..... do something here
};
var d = new Database(false, callback_actOnDatabase);
instead of mongo-factoy use mongoose module to connect the database,model declaration also we dont intalise the db parameters again,please go through the link
https://www.npmjs.com/package/mongoose

Firebase realtime database: writing new data is failing

I have a bunch of data that needs to be updated in my realtime database but the set command (as described here in the docs) isn't working. Here's my code, which I'm running with babel-node scriptName.js:
var config = {
//CONFIG
};
Firebase.initializeApp(config);
var dbRef = Firebase.database().ref();
getAllFirebaseDocs(dbRef);
async function getAllFirebaseDocs(dbRef, newSet) {
var snapshot = await dbRef.once('value');
var data = snapshot.val();
var bookName = "FirebaseForDummies";
var newData = {
'prop1': 'string',
'prop2': 0
}
Firebase.database().ref(bookName + "/edition/"+ 3).set(newData);
}
I'm doing some other stuff which doesn't affect the write (hence reading all existing data). I'm specifically updating/augmenting existing data, so for example; the book FirebaseForDummies has 3 editions already. This means that the editions value in the database looks something like this:
0: {data}
1: {data}
2: {data}
I want to add a fourth, so I create the db reference with FirebaseForDummmies/edition/3. Then just like in the doc, I use set and pass it the newData object. This however fails silently; I don't get any error messages, and nothing changes in the realtime database console.
edit:
I tried these commands in a babel-node session in my console, and they worked. So there's something in my script that's making the set() function not work... not sure what it is, since I have other scripts that implement set() on existing data and they all work fine.
edit2:
I added a callback as follows:
Firebase.database().ref(bookName + "/edition/"+ 3).set(newData, function(error) {
if(error) {
console.log(error);
}
else {
console.log("written!");
}
});
Again, when I try it in a babel-node console it works fine (returning 'written!') but in my script, I don't get either the error or success console.log.
The answer is not to do a process.exit() at the end of the script. I didn't even think of it because I put it there simply to close the script when it was done, but apparently it was 'cutting off' the requests. I think I should make await work with the set requests so that this can all happen synchronously.

Node.js and mongodb access mongodb

I'm trying to set up mongodb on Windows 8 using node.js, Does anyone know why im getting this error. C:\users\phill\node_modules\mongodb\lib\mongodb\mongo_client.js:359 it also says at collection = db collection,,, can't call method 'collection' of null. I'm having a hard time setting it up. My goal is to be able to add to mongo db, and see that I add or pull up what I added, but adding something is good enough for me for now. I'm trying every thing I can find, even straight from the website, I tried everything I see on here as well. Think it maybe it's the way I have things set up. My node.js is saved in my c: drive there is a file that says, program files(86x) in there I have node_modules, npm and such. The path ends up being, computer > windows (C:) > program files(86x) > nodejs. My Mongodb is saved right on my C: drive the path end up being windows (C:) > mongodb-win32-x86_64-2008plus-2.4.8. In my C: I also created a file data and in it created another db. I have been told i should just use mongoose, I'm just learning so i open to any advice, links or anything that will help. I have one last question as well, i learned php and then found out about sql injections and stuff like that, i am not seeing anything about security at all, should i expect the same as well. For this i get text not defined, but i have been getting errors with everthing i have done, best i did was get stuck on a right concern screen.
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect("mongodb://localhost:27017/integration_test", function(err, db) {
test.equal(null, err);
test.ok(db != null);
db.collection("replicaset_mongo_client_collection").update({a:1},
{b:1}, {upsert:true}, function(err, result) {
test.equal(null, err);
test.equal(1, result);
db.close();
test.done();
});
});
Tried this as well and getting a error,C:\users\phill\node_modules\mongodb\lib\mongodb\mongo_client.js:359.... at collection = db collection,,, can't call method 'collection' of null. im calling it in command prompt node filename.js I'm saving it where my node.js file is, I have pulled up files before and created a server.
var Db = require('mongodb').Db,
MongoClient = require('mongodb').MongoClient,
Server = require('mongodb').Server,
ReplSetServers = require('mongodb').ReplSetServers,
ObjectID = require('mongodb').ObjectID,
Binary = require('mongodb').Binary,
GridStore = require('mongodb').GridStore,
Grid = require('mongodb').Grid,
Code = require('mongodb').Code,
BSON = require('mongodb').pure().BSON,
assert = require('assert');
var db = new Db('test', new Server('localhost', 27017));
// Fetch a collection to insert document into
db.open(function(err, db) {
var collection = db.collection("simple_document_insert_collection_no_safe");
// Insert a single document
collection.insert({hello:'world_no_safe'});
// Wait for a second before finishing up, to ensure we have written the item to disk
setTimeout(function() {
// Fetch the document
collection.findOne({hello:'world_no_safe'}, function(err, item) {
assert.equal(null, err);
assert.equal('world_no_safe', item.hello);
db.close();
})
}, 100);
});
In your first code example, you said:
For this i get text not defined
I assume you meant "test not defined?" Your script only requires the mongodb library, and I don't believe test is a core nodejs function, so that would explain the error.
To reference the driver documentation for db.collection(), an assert library is used, but also properly imported (as you did in your second example).
Within your callback to db.open(), you don't check if an error occurred. That might shed some light on why db is null in that function.
Regarding your question about the equivalent of SQL injection with MongoDB, the main areas of concern are places where you might pass untrusted input into evaluated JavaScript, or using such input to construct free-form query objects (not simply using a string, but more like dropping an object into your BSON query). Both of these links should provide more information on the subject:
What type of attacks can be used vs MongoDB?
How does MongoDB address SQL or Query injection?

Categories

Resources