Problem with function node

Hello there,

i'm trying to create a node which uses the mongodb api to connect to a mongodb database.

I inserted mongodb:require('mongodb') in the setting.js and additional "mongodb": "*" in the package.json, don't know if this is required.

I allways get the modules over the global context like var MongoClient = global.get('mongodb').MongoClient.

MongoClient.connect(...) works well i get a database connection and can make operations, but when i try to use a MongoClient object there occures the following error when i call the open function: TypeError: mongodb.open is not a function.

Can anybody please help me?

I think you need to share the actual code of your function node.

Why aren't you using any of the existing MongoDB nodes, like the node-red-node-mongodb? It spares you the hassle of managing everything yourself.

More nodes:
https://flows.nodered.org/search?term=mongo&type=node

var MongoClient = global.get('mongodb').MongoClient;
var Server = global.get('mongodb').Server;

var mongodb = new MongoClient(new Server("localhost", 27017));

  mongodb.open(function(err, mongodb) {

    
  });

Thank you for your answer, i want to make it with a function node.

May I ask why? This way you create a new server connection for each message. And you have to clean-up all resources yourself after that.

The code itself seems to look fine, but I'm not familiar with the Mongo lib.

I guess from your post, you do have this in your settings.js

functionGlobalContext: {
    mongodb:require('mongodb')
}

and that the mongo lib has been installed in your workspace?

The code i showed you is only for the first test....firstly i want to make it run.
MongoDb is installed in ~/.node-red/ and yes my settings.js look like this.

No specific reason i only want to try it :slight_smile:

I think they changed some API in version 3, so your code may not work any more.

Have you tried some up-to-date examples from their documentation?

1 Like

Hey again,

it took a while but now it works fine.

Thank you.

Where can i find the api complete for my version? i've installed mongodb 3.6.3

I searched for mongodb api and it came up with https://api.mongodb.com/ which looks like it might be what you want.

Thanks, but based on what has been discussed here, i search a ACTUAL api.

When you say API you mean you’re looking for a reference of defined functions/properties available? Check the docs on the mongo site, for their JavaScript bindings.

Thanks, but based on what has been discussed here, not everything in the mongo docs/ api isn't obviously correct/ actual. kuema posted a link to the change log on github but i cann't find these changes in the current api.

It's all there. The link I posted earlier was the changelog, because I suspected your example might have been out of date.

http://mongodb.github.io/node-mongodb-native/

1 Like

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