Mongodb api and node red

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

Because the connect function is resolved asynchronously, meaning that the line calling mongoc.db can run before mongoc is actually populated.
Another tip, even though you’re connecting to a mongo server on localhost, but change your password and remove it from the post above.

Okay, thank you....when i try to use await node red allways displays errors:
"SyntaxError: await is only valid in async function". That's why i thought it is synchronous.
Thanks, i thought about it before i posted it, but it will never be production system.

The outer function node code is synchronous, but capable of handling async code the same way you would do when writing ES6 compatible code yourself. Meaning you can use await, but it has to be inside an async function. Which in term can be run either with promises or callbacks. In order to output from the function node asynchronously, check the docs: https://nodered.org/docs/user-guide/writing-functions

See further this topic which has an example of async function calls in a function node: Async function - getting error: TypeError: is not a constructor

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.