metric_caution
metric_caution

Reputation: 131

Azure Functions crashing loading MongoDB in Node

I'm attempting to write an Azure Function, in Node, to connect into a MongoDB instance (Cosmos DB in this case).

However, as soon I run require("mongodb"), my function crashes, without throwing an error, or logging anything, with the HTTP response returning a 502 code.

My setup:

This doesn't throw an error in the code, and I see logging that's run before, but not after the require statement (which is making it pretty difficult to debug).

I've also tried following through this guide about running a mongo query from a function, and it fails in exactly the same way for me.

After putting some hooks into Node's module module, my attempts to debug this led to a line in one of mongo's dependencies that fails in a similar way when run in isolation (from saslprep), which seems to stem from running out of stack space.

However, this feels like its a pretty mainstream use for an Azure function, and I haven't seen any similar issues, so I'm inclined to suspect that its an issue with my setup, rather than the mongodb library, but I haven't been able to find a misconfiguration, as I haven't changed any defaults - right now, I'm stumped!

I don't have a full code example right now, as I'm away from my work computer, but the code is something like

const mongo = require('mongodb');
module.exports = function(context) {
  context.res = {
    body: 'Hello world'
  };

  context.done();
}

Without the require statement, the code runs fine, returning the response to the browser.

Upvotes: 1

Views: 436

Answers (2)

metric_caution
metric_caution

Reputation: 131

It turns out that this problem was caused by running out of stack space. After pushing a patch to the saslprep library (v1.0.1), this has now been fixed.

Upvotes: 1

MarkR
MarkR

Reputation: 188

Im pretty sure that if you add to your require function the same as in Microsofts Cosmos DB guides for mongo the following it should work

var mongodb = require('mongodb').MongoClient;

you have it as:

const mongodb = require('mongodb');

Im curious to know if that makes a difference. After looking through Microsofts own docs nearly all of them are declared that way.

Here is the tutorial I found: MongoDB app using Node.js

Upvotes: 0

Related Questions