4

Mongoose return a empty array when i am trying to use find() function.Can any one help me with this please?

users.js

const mongoose = require('mongoose');
const Schema = mongoose.Schema;    
mongoose.Promise = global.Promise;

const userSchema = new Schema({
    name: String    
});
const user = mongoose.model('user', userSchema, 'users');    
module.exports = user;

test_helper.js

   const mongoose = require('mongoose');

    mongoose.connect('mongodb://localhost/user_test');

    before((done)=>{
        mongoose.connection.once('open',()=>{
            console.log('Connected');
        }).on('error', ()=>{
            console.log('Error');
        });
        done();
    });

    beforeEach((done)=>{
        mongoose.connection
                .collections
                .users
                .drop(()=>{
            done();
        });
     });

create_user.js

const assert = require('assert');
const User = require('../src/user');


describe('Create a user',()=>{
    it('it should create a new user',(done)=>{
        let joe = new User({
            name : "Joe"
        });
        joe.save().then(()=>{
            assert(!joe.isNew);
            done();
        });
    });
})

reading-users.js

const assert = require('assert');
const User = require('../src/user');


describe('This will read all users', () => {
    before((done) => {
        joe = new User({name:'joe'});
        joe.save().then(() => done());
    });
    it('It Should find all users named joe', (done) => {
        User.findOne({name:"Joe"})
            .then((foo)=>{
                //assert(users[0]._id.toString()===joe._id.toString());
                console.log(foo);
                console.log(joe);
                done();
        });
    });
});

=============================log==========================================

(node:3324) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect. Create a user Connected √ it should create a new user (274ms)

This will read all users [] { _id: 5bfa98eb6bd05d0cfc5d639d, name: 'joe', __v: 0 } √ It Should find all users named joe

2 passing (1s)

11
  • 2
    Nothing here should be returning an "array". I presume you have probably changed the code several times from find() to findOne()? Being the former returns an array but the other does not. Add mongoose.set('debug', true) at the top of your tests in order to see the output of what is actually being sent to MongoDB. And most importantly "when"! Commented Nov 25, 2018 at 12:34
  • a typo: 'joe' and 'Joe' are different Commented Nov 25, 2018 at 12:38
  • @piisexactly3 The "difference" is in the description of the test, not in the data or query. So that's not it. Commented Nov 25, 2018 at 12:43
  • @Neil Lunn...I tried but had zero result Commented Nov 25, 2018 at 12:43
  • @piisexactly3 corrected it but still same...I have worked in this since two days..can anyone help me please Commented Nov 25, 2018 at 12:44

1 Answer 1

3

The root cause for this problem is that every time you start your test, created records will always removed before your test get started. This is occurring inside the beforeEach block in your test_helper.js file. try to replacebeforeEach with afterEach.

afterEach((done)=>{
    mongoose.connection.collections.users.drop(()=>{
        done();
    });
});
Sign up to request clarification or add additional context in comments.

Comments

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.