# Problem with function node

**URL:** <https://discourse.nodered.org/t/problem-with-function-node/21687>\
**Category:** General\
**Created:** [14 February 2020 09:45 UTC](https://discourse.nodered.org/t/problem-with-function-node/21687 "2020-02-14T09:45:04Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![tobias](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tobias/32/17195_2.png) [@tobias](https://discourse.nodered.org/u/tobias)\
**Post date:** [14 February 2020 09:45 UTC](https://discourse.nodered.org/t/problem-with-function-node/21687/1 "2020-02-14T09:45:04Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [14 February 2020 09:57 UTC](https://discourse.nodered.org/t/problem-with-function-node/21687/2 "2020-02-14T09:57:12Z")

</div>

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](https://flows.nodered.org/node/node-red-node-mongodb)? It spares you the hassle of managing everything yourself.

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

---

<div class="post-metadata">

**Author:** ![tobias](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tobias/32/17195_2.png) [@tobias](https://discourse.nodered.org/u/tobias)\
**Post date:** [14 February 2020 10:07 UTC](https://discourse.nodered.org/t/problem-with-function-node/21687/3 "2020-02-14T10:07:03Z")

</div>

```auto
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.

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [14 February 2020 10:15 UTC](https://discourse.nodered.org/t/problem-with-function-node/21687/4 "2020-02-14T10:15:27Z")

</div>

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

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

```

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

---

<div class="post-metadata">

**Author:** ![tobias](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tobias/32/17195_2.png) [@tobias](https://discourse.nodered.org/u/tobias)\
**Post date:** [14 February 2020 10:22 UTC](https://discourse.nodered.org/t/problem-with-function-node/21687/5 "2020-02-14T10:22:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tobias](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tobias/32/17195_2.png) [@tobias](https://discourse.nodered.org/u/tobias)\
**Post date:** [14 February 2020 10:23 UTC](https://discourse.nodered.org/t/problem-with-function-node/21687/6 "2020-02-14T10:23:24Z")

</div>

No specific reason i only want to try it 🙂

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [14 February 2020 10:28 UTC](https://discourse.nodered.org/t/problem-with-function-node/21687/7 "2020-02-14T10:28:08Z")

</div>

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

> <https://github.com/mongodb/node-mongodb-native/blob/HEAD/CHANGES_3.0.0.md>

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

---

<div class="post-metadata">

**Author:** ![tobias](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tobias/32/17195_2.png) [@tobias](https://discourse.nodered.org/u/tobias)\
**Post date:** [14 February 2020 12:08 UTC](https://discourse.nodered.org/t/problem-with-function-node/21687/8 "2020-02-14T12:08:21Z")

</div>

Hey again,

it took a while but now it works fine.

Thank you.

---

<div class="post-metadata">

**Author:** ![tobias](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tobias/32/17195_2.png) [@tobias](https://discourse.nodered.org/u/tobias)\
**Post date:** [17 February 2020 12:39 UTC](https://discourse.nodered.org/t/problem-with-function-node/21687/9 "2020-02-17T12:39:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [17 February 2020 13:42 UTC](https://discourse.nodered.org/t/problem-with-function-node/21687/10 "2020-02-17T13:42:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tobias](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tobias/32/17195_2.png) [@tobias](https://discourse.nodered.org/u/tobias)\
**Post date:** [17 February 2020 13:49 UTC](https://discourse.nodered.org/t/problem-with-function-node/21687/11 "2020-02-17T13:49:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [17 February 2020 13:59 UTC](https://discourse.nodered.org/t/problem-with-function-node/21687/12 "2020-02-17T13:59:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tobias](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tobias/32/17195_2.png) [@tobias](https://discourse.nodered.org/u/tobias)\
**Post date:** [17 February 2020 14:13 UTC](https://discourse.nodered.org/t/problem-with-function-node/21687/13 "2020-02-17T14:13:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)\
**Post date:** [17 February 2020 15:17 UTC](https://discourse.nodered.org/t/problem-with-function-node/21687/14 "2020-02-17T15:17:27Z")

</div>

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/](http://mongodb.github.io/node-mongodb-native/)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [2 March 2020 15:17 UTC](https://discourse.nodered.org/t/problem-with-function-node/21687/15 "2020-03-02T15:17:35Z")

</div>

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