Can not connect Azure Functions to MongoDB (created in azure)

by Terungwa   Last Updated October 01, 2018 19:03 PM

I have a MongoDB setup on azure, and I am tring to connect to it via azure function.

These are the steps I took:

  1. Creating a Simple Azure Function
  2. Installed the MongoDB Driver on Azure, To install the MongoDB Node.js driver, I went go to, and clicked on 'Debug Console' -> 'PowerShell'.
  3. I Navigated to the D:\home\site\wwwroot directory and clicked on the plus icon to create a new file called package.json.
  4. I Created and saved the below package.json file.

      "name": "nameofunction",
      "dependencies": {
       "mongodb": "3.x"
  5. Next, I ran npm install from the shell.
  6. From the Azure Function I should be able to connect to MongoDB and execute a query using the below code.

    const  mongodb = require('mongodb');
    const  url = "mongodb://cosmod:  <PASSWORD>[email protected]:10255/?ssl=true&replicaSet=globaldb";
    module.exports = async function (context, req) {
        mongodb.connect(url, function(error, client) {        
          if (error) throw error;
          var dbo = client.db("mydb");
          dbo.createCollection("customers", function(err, res) {
             if (err) throw err;
             context.log("Collection created!");

My code is throwing up a Status: 500 Internal Server Error

The more I look at the code, the more i can not understand why this should not work. The package-lock.jsonhas all the dependencies loaded after I ran npm install in the shell.

I appreciate any help in resolving this.

Tags : azure

Related Questions

Security data web application

Updated April 26, 2016 08:01 AM