Hello,
msg.topic = "Initialization of db";
var MongoClient = global.get('mongodb').MongoClient;
var Server = global.get('mongodb').Server;
var db;
try {
new MongoClient(new Server('localhost', 27017), {
user: 'node_red_admin',
password: ':)',
authSource: 'node_red_db1'}).connect((err, client) => {
if(err) {
console.error(err);
msg.errClientCon = err;
}
db = client.db('node_red_db1');
db.dropCollection('twitter').catch(err => {
if(err) console.error("ERR: " + err);
});
db.createCollection('twitter', {capped: true, size: 500000, max: 100}, (err, col) => {
if(err) console.error("ERR: " + err);
});
});
} catch(ex) {
msg.errEx = ex.message;
}
return msg;
This works fine.
msg.topic = "Initialization of db";
var MongoClient = global.get('mongodb').MongoClient;
var Server = global.get('mongodb').Server;
var mongoc;
var db;
try {
new MongoClient(new Server('localhost', 27017), {
user: 'node_red_admin',
password: ':(',
authSource: 'node_red_db1'}).connect((err, client) => {
mongoc = client;
});
var mongoc = global.get('mongoc');
db = mongoc.db('node_red_db1');
db.dropCollection('twitter').catch(err => {
if (err) console.error("ERR: " + err);
});
db.createCollection('twitter', { capped: true, size: 500000, max: 100 }, (err, col) => {
if (err) console.error("ERR: " + err);
});
} catch(ex) {
msg.errEx = ex.message;
}
return msg;
This doesn't. But i cann't explain why, anybody else?
The error message is: "Cannot read property 'db' of undefined".
My intention is to store the client
in the global context to use it in other nodes as well, but when it's not even possible to store it in a local variable (mongoc
in my case) it will not be possible.
Best regards, Tobias