Mongoose return a empty array












3















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)











share|improve this question




















  • 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"!

    – Neil Lunn
    Nov 25 '18 at 12:34













  • a typo: 'joe' and 'Joe' are different

    – piisexactly3
    Nov 25 '18 at 12:38











  • @piisexactly3 The "difference" is in the description of the test, not in the data or query. So that's not it.

    – Neil Lunn
    Nov 25 '18 at 12:43











  • @Neil Lunn...I tried but had zero result

    – Steave Jones
    Nov 25 '18 at 12:43











  • @piisexactly3 corrected it but still same...I have worked in this since two days..can anyone help me please

    – Steave Jones
    Nov 25 '18 at 12:44
















3















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)











share|improve this question




















  • 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"!

    – Neil Lunn
    Nov 25 '18 at 12:34













  • a typo: 'joe' and 'Joe' are different

    – piisexactly3
    Nov 25 '18 at 12:38











  • @piisexactly3 The "difference" is in the description of the test, not in the data or query. So that's not it.

    – Neil Lunn
    Nov 25 '18 at 12:43











  • @Neil Lunn...I tried but had zero result

    – Steave Jones
    Nov 25 '18 at 12:43











  • @piisexactly3 corrected it but still same...I have worked in this since two days..can anyone help me please

    – Steave Jones
    Nov 25 '18 at 12:44














3












3








3


1






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)











share|improve this question
















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)








javascript node.js mongodb mongoose






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 12:52







Steave Jones

















asked Nov 25 '18 at 12:28









Steave JonesSteave Jones

635




635








  • 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"!

    – Neil Lunn
    Nov 25 '18 at 12:34













  • a typo: 'joe' and 'Joe' are different

    – piisexactly3
    Nov 25 '18 at 12:38











  • @piisexactly3 The "difference" is in the description of the test, not in the data or query. So that's not it.

    – Neil Lunn
    Nov 25 '18 at 12:43











  • @Neil Lunn...I tried but had zero result

    – Steave Jones
    Nov 25 '18 at 12:43











  • @piisexactly3 corrected it but still same...I have worked in this since two days..can anyone help me please

    – Steave Jones
    Nov 25 '18 at 12:44














  • 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"!

    – Neil Lunn
    Nov 25 '18 at 12:34













  • a typo: 'joe' and 'Joe' are different

    – piisexactly3
    Nov 25 '18 at 12:38











  • @piisexactly3 The "difference" is in the description of the test, not in the data or query. So that's not it.

    – Neil Lunn
    Nov 25 '18 at 12:43











  • @Neil Lunn...I tried but had zero result

    – Steave Jones
    Nov 25 '18 at 12:43











  • @piisexactly3 corrected it but still same...I have worked in this since two days..can anyone help me please

    – Steave Jones
    Nov 25 '18 at 12:44








2




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"!

– Neil Lunn
Nov 25 '18 at 12:34







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"!

– Neil Lunn
Nov 25 '18 at 12:34















a typo: 'joe' and 'Joe' are different

– piisexactly3
Nov 25 '18 at 12:38





a typo: 'joe' and 'Joe' are different

– piisexactly3
Nov 25 '18 at 12:38













@piisexactly3 The "difference" is in the description of the test, not in the data or query. So that's not it.

– Neil Lunn
Nov 25 '18 at 12:43





@piisexactly3 The "difference" is in the description of the test, not in the data or query. So that's not it.

– Neil Lunn
Nov 25 '18 at 12:43













@Neil Lunn...I tried but had zero result

– Steave Jones
Nov 25 '18 at 12:43





@Neil Lunn...I tried but had zero result

– Steave Jones
Nov 25 '18 at 12:43













@piisexactly3 corrected it but still same...I have worked in this since two days..can anyone help me please

– Steave Jones
Nov 25 '18 at 12:44





@piisexactly3 corrected it but still same...I have worked in this since two days..can anyone help me please

– Steave Jones
Nov 25 '18 at 12:44












1 Answer
1






active

oldest

votes


















1














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();
});
});





share|improve this answer
























  • Thank you @Aravinda You save my day man...

    – Steave Jones
    Nov 25 '18 at 16:14











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53467441%2fmongoose-return-a-empty-array%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














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();
});
});





share|improve this answer
























  • Thank you @Aravinda You save my day man...

    – Steave Jones
    Nov 25 '18 at 16:14
















1














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();
});
});





share|improve this answer
























  • Thank you @Aravinda You save my day man...

    – Steave Jones
    Nov 25 '18 at 16:14














1












1








1







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();
});
});





share|improve this answer













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();
});
});






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 25 '18 at 15:51









Aravinda MeewalaarachchiAravinda Meewalaarachchi

6271512




6271512













  • Thank you @Aravinda You save my day man...

    – Steave Jones
    Nov 25 '18 at 16:14



















  • Thank you @Aravinda You save my day man...

    – Steave Jones
    Nov 25 '18 at 16:14

















Thank you @Aravinda You save my day man...

– Steave Jones
Nov 25 '18 at 16:14





Thank you @Aravinda You save my day man...

– Steave Jones
Nov 25 '18 at 16:14




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53467441%2fmongoose-return-a-empty-array%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin