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

2

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

6 Comments

I couldn't understand. I already declared mongo_url which includes db host, credentials and name
maybe you simply have to put in your url db host and credentials
I log it to check, mongo_url returns mongodb://localhost/mydb, which does fit connection string that includes host, credentials, and name
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
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
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.