Mongoose find() Method Returns No Value First Time I Run Node Mongoose find() Method Returns No Value First Time I Run Node mongoose mongoose

Mongoose find() Method Returns No Value First Time I Run Node


find() method runs before the database has the chance to store the data.
You can store the insertMany() call in an async function with await and put your Fruit.find() after it

async function myFunc() {    await Fruit.insertMany([apple, kiwi, orange, banana], function (error) {        if (error) {            console.log(error);        } else {            console.log('New entries are added to the database!');        }    });    Fruit.find(function (err, fruits) {        if (err) {            console.log(err);        } else {            console.log(fruits);        }    });}myFunc();

Or take advantage of Promise callbacks and use .then().

myFunc().then(() => {    Fruit.find(function (err, fruits) {        if (err) {            console.log(err);        } else {            console.log(fruits);        }    });},err=> console.log(err));


You have to wait for Mongoose to connect to your MongoDB server, and then you should perform all the logic. What you can do is just put all the logic in the main() function after the await mongoose.connect('mongodb://localhost:27017/fruitsDB');. That will ensure that the Mongoose connected to the MongoDB server before any DB query where executed.

const mongoose = require('mongoose');main().catch(err => console.log(err));// This section means connect to mongodb and create fruitsDB database inside thatasync function main() {  await mongoose.connect('mongodb://localhost:27017/fruitsDB');  const fruitSchema = new mongoose.Schema({    name: String,    rating: Number,    review: String  });  const Fruit = mongoose.model('Fruit', fruitSchema);  //  MongoDB will automatically pluralize the collection name  // const fruit = new Fruit({  //     name: 'Apple',  //     rating: 7,  //     review: 'Pretty solid as fruit!'  // });  // Comment out this section to prevent mongoose to   // save fruit to fruits collection everytime you run server.js  // fruit.save();  const peopleSchema = new mongoose.Schema({    name: String,    age: Number  });  const People = mongoose.model('People', peopleSchema);  const people = new People({    name: 'John',    age: 37  });  people.save();  const apple = new Fruit({    name: 'Apple',    rating: 7,    review: 'Pretty solid as fruit!'  });  const kiwi = new Fruit({    name: 'Kiwi',    rating: 10,    review: 'The best fruit!'  });  const orange = new Fruit({    name: 'Orange',    rating: 4,    review: 'Too sour for me!'  });  const banana = new Fruit({    name: 'Banana',    rating: 3,    review: 'Weird texture!'  });  Fruit.insertMany([apple, kiwi, orange, banana], function(error) {    if (error) {      console.log(error);    } else {      console.log('New entries are added to the database!');    }  });  // Read the fruits collection  Fruit.find(function(err, fruits) {    if (err) {      console.log(err);    } else {      console.log(fruits);    }  });}