0

I have a problem with my code. I am using Mocha+Chai+Mongodb. When I run de code this mesage show: "TypeError: Cannot read property 'collections' of undefined"

My code for test is this:

    const chai = require('chai');
const calculadora = require('../../db');

const assert = chai.assert;

describe('TDD de Operações da Calculadora', () => {
  it('Teste: Deve Somar 2 Números', () => {
    assert.equal(calculadora.insert('Carol', 20, 'MG'),true);
  });});

And my .js is this:

    var ObjectId = require("mongodb").ObjectId;
var mongoClient = require("mongodb").MongoClient;

mongoClient.connect("mongodb://localhost:56467/locadora")
            .then(conn => global.conn = conn.db("locadora"))
            .catch(err => console.log(err))

function findAll(callback){
    global.conn.collection('customers').find({}).toArray(callback);
}

module.exports = { findAll}

Someone could help me please? Thanks.

  • You are setting `global.conn` inside an async function, so it isn't defined by the time `findAll` is processed. – Adam Oct 03 '18 at 12:32
  • Possible duplicate of [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – Adam Oct 03 '18 at 12:44

0 Answers0