Node application fails to connect to MongoDB

mongo
nodejs
npm

#1

I’m getting the following error when trying to run my tests:

    1) Uncaught error outside test suite
Unhandled rejection MongoError: failed to connect to server [127.0.0.1:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]
    at Pool.<anonymous> (node_modules/mongodb-core/lib/topologies/server.js:336:35)
    at Connection.<anonymous> (node_modules/mongodb-core/lib/connection/pool.js:280:12)
    at Socket.<anonymous> (node_modules/mongodb-core/lib/connection/connection.js:187:49)
    at emitErrorNT (net.js:1279:8)
    at _combinedTickCallback (internal/process/next_tick.js:80:11)
    at process._tickCallback (internal/process/next_tick.js:104:9)

My config.yml looks like this:

# Javascript Node CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-javascript/ for more details
#
version: 2

dependencies:
  cache_directories:
    - mongodb-linux-x86_64-ubuntu1204-3.2.0
  pre:
    - if [[ ! -d mongodb-linux-x86_64-ubuntu1204-3.2.0 ]]; then wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1204-3.2.0.tgz && tar xvzf mongodb-linux-x86_64-ubuntu1204-3.2.0.tgz; fi
    - sudo stop mongodb
    - sudo cp mongodb-linux-x86_64-ubuntu1204-3.2.0/bin/* /usr/bin
    - sudo start mongodb

machine:
  services:
    - mongodb

jobs:
  build:
    docker:
      # specify the version you desire here
      - image: circleci/node:7.10

      # Specify service dependencies here if necessary
      # CircleCI maintains a library of pre-built images
      # documented at https://circleci.com/docs/2.0/circleci-images/
      # - image: circleci/mongo:3.4.4

    working_directory: ~/repo

    steps:
      - checkout

      # Download and cache dependencies
      - restore_cache:
          keys:
          - v1-dependencies-{{ checksum "package.json" }}
          # fallback to using the latest cache if no exact match is found
          - v1-dependencies-

      - run: npm install

      - save_cache:
          paths:
            - node_modules
          key: v1-dependencies-{{ checksum "package.json" }}

      # run tests!
      - run: npm test

And my mongo initialization looks like this:

const mongoose = require('mongoose');
mongoose.Promise = require('bluebird');

mongoose.connect('mongodb://127.0.0.1:27017/projectname');

module.exports = mongoose;

My guess is that I need to use a different database URI, but I can’t seem to find the correct one.

Any help appreciated, thanks!


#2

Why is the mongo container commented out?


#3

Oh I didn’t realize, that was the version that was on their example. I removed the comment and it works!


#4