2

This is my FollowerModel class (in ES6) representing follower collection of my MongoDB

import mongo from 'mongodb';

class FollowerModel {

    constructor(db, logger) {
        this._db = db;
        this._logger = logger;
    }

    async create(data) {
        try {
            data._id = mongo.ObjectID().toString();
            return await this._db.collection('follower').insertOne(data);
        } catch (error) {
            return Promise.reject(error);
        }
    }

}

export default FollowerModel;

And this is my unit test

import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import { expect } from 'chai';
import mongo from 'mongodb';
import mongo_url from '../../config/mongodb';
import logger from '../../config/logger';
import to from 'await-to-js';
import FollowerModel from '../../model/follower';

const MongoClient = mongo.MongoClient;
const data = {
    email: 'my_email',
    phone: 'my_phone',
    code: 'my_code'
};

describe('Model: Follower', () => {

    let Follower;
    let connected = false;
    let _db;

    const connect = () => {
        try {
            const db = await MongoClient.connect(mongo_url);
            Follower = new FollowerModel(db, logger);
            connected = true;
            _db = db;
            return Promise.resolve();
        } catch (error) {
            return Promise.reject(error);
        }
    };

    before(() => connect());

    describe('create', () => {
        let _id;
        beforeEach(() => connected ? null : connect());
        it('Returns with insertedId', async () => {
            const [, result] = await to(Follower.create(data));
            return expect(result).to.have.property('insertedId');
        });
        afterEach(() => connected ?
            _db.collection('follower').deleteOne({ _id }) : connect().then(() => _db.collection('follower').deleteOne({ _id }))
        );
    });

});

This test fails, the error is this._db.collection is not a function much to my frustration. As I initialize Follower instance, db is passed to the constructor, initializing db successfully in class. I even tried to console.log both db and db.collection. It does log db as expected but returns undefined for db.collection. This confuses me

necroface
  • 3,365
  • 10
  • 46
  • 70
  • `_db.collection` indicates that you `_db` is not a database – edkeveked Dec 22 '17 at 07:40
  • Possible duplicate of [db.collection is not a function when using MongoClient v3.0](https://stackoverflow.com/questions/47662220/db-collection-is-not-a-function-when-using-mongoclient-v3-0) – Mika Sundland Dec 22 '17 at 11:00

1 Answers1

1

Since the V3+ of the MongoDB native NodeJS driver:

const db = await MongoClient.connect(mongo_url);

_db = db.db("nameOfyourDataBase");

//then you can use _db.collection
edkeveked
  • 17,989
  • 10
  • 55
  • 93
  • I couldn't understand. I already declared `mongo_url` which includes db host, credentials and name – necroface Dec 22 '17 at 07:41
  • maybe you simply have to put in your url db host and credentials – edkeveked Dec 22 '17 at 07:43
  • I log it to check, `mongo_url` returns `mongodb://localhost/mydb`, which does fit connection string that includes host, credentials, and name – necroface Dec 22 '17 at 07:45
  • It did, much to my surprise. But I want to understand why it happened. I used this same practice in my old project, which worked perfectly – necroface Dec 22 '17 at 07:49
  • As I inspected, this is a bug of new version of `node-mongodb-native` package. When I switched to the old version of my old project (`v2.2.28`) instead of the newest `v3.0.0`, it worked perfectly – necroface Dec 22 '17 at 07:53
  • Yes, since the v3+, maybe it is a bug or the way to go :) – edkeveked Dec 22 '17 at 07:58